使用jQuery和scrollTo插件进行Scrollspy

时间:2014-09-18 17:51:11

标签: javascript jquery html css

我正在尝试在我正在开发的网站上实施ScrollSpy。

这是我在

之后的教程的链接

Tutorial

这里是演示:

Demo

我试图根据自己的需要调整脚本,但我一直在javascript上出错,控制台说"无法读取属性' top'未定义"

这是我的Javascript

*/$(document).ready(function(){


$(".menu li a").click(function(evn){
    evn.preventDefault();
    $('html,body').scrollTo(this.hash, this.hash);
});

/**
 * This part handles the highlighting functionality.
 * We use the scroll functionality again, some array creation and
 * manipulation, class adding and class removing, and conditional testing
 */
var aChildren = $(".menu li").children(); // find the a children of the list items
var aArray = []; // create the empty aArray
for (var i=0; i < aChildren.length; i++) {
    var aChild = aChildren[i];
    var ahref = $(aChild).attr('href');
    aArray.push(ahref);
} // this for loop fills the aArray with attribute href values

$(window).scroll(function(){
    var windowPos = $(window).scrollTop(); // 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];
        if (theID.length) {
            var divPos = $(theID).offset().top;
            var divHeight = $(theID).height(); // get the height of the div in question
            if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
                $("a[href='" + theID + "']").parent().addClass("active");
            } else {
                $("a[href='" + theID + "']").parent().removeClass("active");
            }
        }
    }

    if(windowPos + windowHeight == docHeight) {
        if (!$(".menu li:last-child a").hasClass("active")) {
            var navActiveCurrent = $(".active").attr("href");
            $("a[href='" + navActiveCurrent + "']").removeClass("active");
            $(".menu li:last-child a").addClass("active");
        }
    }
}); });

这是我正在开发的网站

Web-Site

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

问题是你使用href作为jQuery的选择器。你需要形成一个正确的选择器:

   for (var i=0; i < aArray.length; i++) {
    var theID = aArray[i];
    if (theID.length) {
        var divPos = $(theID).offset().top;
        var divHeight = $(theID).height(); // get the height of the div in question
        if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
            $("a[href='" + theID + "']").parent().addClass("active");
        } else {
            $("a[href='" + theID + "']").parent().removeClass("active");
        }
    }
}

应改为:

   for (var i=0; i < aArray.length; i++) {
    var theID = aArray[i];
    var selector = "a[href='" + theID + "']";
    if (theID.length) {
        var divPos = $(selector).offset().top;
        var divHeight = $(selector).height(); // get the height of the div in question
        if (windowPos >= divPos && windowPos < (divPos + divHeight)) {
            $(selector).parent().addClass("active");
        } else {
            $(selector).parent().removeClass("active");
        }
    }
}
相关问题