2011-12-21 57 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