0
我在一頁滾動網站上使用此代碼的工作,但我已經將整個網址添加到href,如「href =」index.php#home「,而不是隻使用。散列現在我的工作狀態是不工作我如何修改這個代碼,這個工作導航滾動突出顯示
//Navigation Scroll/Highlight
var aChildren = $(".nav li.link").children();
var aArray = [];
for (var i=0; i < aChildren.length; i++) {
var aChild = aChildren[i];
var ahref = $(aChild).attr('href');
aArray.push(ahref);
}
$(window).scroll(function(){
var windowPos = $(window).scrollTop() + 140; // get the offset of the window from the top of page
var windowHeight = $(window).height(); // get the height of the window
var docHeight = $(document).height();
for (var i=0; i < aArray.length; i++) {
var theID = aArray[i];
var divPos = $(theID).offset().top; // get the offset of the div from the top of page
var divHeight = $(theID).height(); // get the height of the div in question
if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
$("a[href='" + theID + "']").addClass("nav-active");
} else {
$("a[href='" + theID + "']").removeClass("nav-active");
}
}
if((windowPos + windowHeight) >= (docHeight - 140)) {
if (!$(".nav li.link:nth-last-child(2) a").hasClass("nav-active")) {
var navActiveCurrent = $(".nav-active").attr("href");
$("a[href='" + navActiveCurrent + "']").removeClass("nav-active");
$(".nav li.link:nth-last-child(2) a").addClass("nav-active");
}
}
});
這將是更好地打破註釋掉的代碼它將提供更好的可讀性,您不必將所有代碼放在一個大的部分中,也可以將其分解成多個部分。 – Madness 2015-08-05 03:19:05