2011-12-21 77 views
0

我想补充取决于滚动位置的主动类感谢,我发现这已经张贴:的jQuery else和if语句上滚动条位置帮助

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"); 
} 
}); 

我得到两个活动类,我已经使用了两个其他ifs。

许多在此先感谢。

回答

1

试试这个:

if (scroll >= 1500) { 
    $("#nav-portfolio").addClass("active"); 
} else if (scroll >= 1000) { 
    $("#nav-about").addClass("active-purple"); 
} else if (scroll >= 500) { 
    $("#nav-welcome").addClass("active"); 
} else { 
    $("#nav-contact").addClass("active-purple"); 
} 
+0

感谢您的答复。虽然它正在激活最后一个else语句 - nav-contact介于nav-welcome和nav-about之间滚动,当滚动备份它时,它会激活nav-contact而不是nav-welcome。 – sarah3585 2011-12-21 22:19:47

+0

你设置的方式有点令人困惑。当你在页面的顶部时,你想要什么? – 2011-12-22 15:30:22