這似乎與您的網站的工作:
var $sections = $('section'); // all content sections
var $navs = $('nav > ul > li'); // all nav sections
var topsArray = $sections.map(function() {
return $(this).position().top - 300; // make array of the tops of content
}).get(); // sections, with some padding to
// change the class a little sooner
var len = topsArray.length; // quantity of total sections
var currentIndex = 0; // current section selected
var getCurrent = function(top) { // take the current top position, and see which
for(var i = 0; i < len; i++) { // index should be displayed
if(top > topsArray[i] && topsArray[i+1] && top < topsArray[i+1]) {
return i;
}
}
};
// on scroll, call the getCurrent() function above, and see if we are in the
// current displayed section. If not, add the "selected" class to the
// current nav, and remove it from the previous "selected" nav
$(document).scroll(function(e) {
var scrollTop = $(this).scrollTop();
var checkIndex = getCurrent(scrollTop);
if(checkIndex !== currentIndex) {
currentIndex = checkIndex;
$navs.eq(currentIndex).addClass("selected").siblings(".selected").removeClass("selected");
}
});
不要混淆你的網址,這不是慌張,讓你看起來像你試圖發送垃圾郵件。我在一個沙箱中打開了這個URL,以確保你沒有試圖欺騙別人(我很高興你沒有)。 – Incognito 2010-10-08 18:16:25
我使用了我的編輯權限並且未對URL進行模糊處理。享受,user257493。 :) – aem 2010-10-08 19:29:51