如何使用 bootstrap scrollspy 将 class 添加到活动菜单项?

How can I add a class to the active menu item using bootstrap scrollspy?

我正在使用这段代码,但它并没有像我预期的那样工作。

$("#myNavbar").on("activate.bs.scrollspy", function(){
    $(this).parent('li').addClass('hvr-overline-reveal');
    var currentItem = $(".nav li.active > a");
    currentItem.parent("li").addClass("hvr-pulse");
    currentItem.addClass("hvr-overline-reveal");
}) 
});

主要问题是:

$("#myNavbar").on("activate.bs.scrollspy", function(){
}

它会在您滚动页面时激活 jQuery。如何在 li 元素出现时添加 class?

https://jsfiddle.net/Hnk9W/12/

使用此代码

$('body').scrollspy({ target: '#navbar-example' });

用您的导航栏替换导航栏示例 阅读文档:http://getbootstrap.com/javascript/#scrollspy