【发布时间】:2011-12-21 19:30:21
【问题描述】:
我想根据滚动位置添加一个活动类,感谢我发现这个已经发布了:
jQuery : add css class to menu item based on browser scroller position
我对本教程有一个额外的导航项,但不知道如何安排 if、else if 和 else 语句。
$(window).scroll(function() {
// find the li with class 'active' and remove it
$("#navigation li.active").removeClass("active");
// get the amount the window has scrolled
var scroll = $(window).scrollTop();
// add the 'active' class to the correct li based on the scroll amount
if (scroll <= 500) {
$("#nav-welcome").addClass("active");
}
else if (scroll <= 1000) {
$("#nav-about").addClass("active-purple");
}
else if (scroll <= 1500) {
$("#nav-portfolio").addClass("active");
}
else {
$("#nav-contact").addClass("active-purple");
}
});
我得到了两个活动课程,其中我使用了两个 else if。
非常感谢。
【问题讨论】:
标签: jquery