Javascript 无法读取未定义jquery滚动的顶部

Javascript 无法读取未定义jquery滚动的顶部,javascript,jquery,scroll,Javascript,Jquery,Scroll,我试图找到一个解决这个错误的方法,但是运气不好,所以我将发布我的具体问题。我有一个jquery滚动条,滚动到菜单中的hashtags,当到达特定的目标hashtags时,它应该将菜单项的颜色从灰色更改为白色。只要有一个小错误,一切正常 当您单击菜单项时,它会向下滚动,但您必须再滚动1-5px才能使脚本将菜单项更改为活动(白色),控制台还会给我一个提示“无法读取未定义的属性'top'。我不清楚,因为脚本将菜单项收集在一个数组中,如果我将console.log记录在数组中,那么这些项就在那里。如果I

我试图找到一个解决这个错误的方法,但是运气不好,所以我将发布我的具体问题。我有一个jquery滚动条,滚动到菜单中的hashtags,当到达特定的目标hashtags时,它应该将菜单项的颜色从灰色更改为白色。只要有一个小错误,一切正常

当您单击菜单项时,它会向下滚动,但您必须再滚动1-5px才能使脚本将菜单项更改为活动(白色),控制台还会给我一个提示“无法读取未定义的属性'top'。我不清楚,因为脚本将菜单项收集在一个数组中,如果我将console.log记录在数组中,那么这些项就在那里。如果I console.log记录div的高度和位置,它会返回值,但仍会给出错误消息

<div class="header">
<div id="nav-anchor"></div>
    <div class="container-fluid menu-content">
        <div class="row">
            <div class="col-md-6">
                <img src="images/logotype.png" alt="Logotype" title="" class="brand">
            </div><!-- end col md 6 brand -->
            <div class="col-md-6 menu">
            <nav>
                <ul>
                    <li><a href="#about">About</a></li>
                    <li><a href="#work">Work</a></li>
                    <li><a href="#cases">Cases</a></li>
                    <li><a href="#meet">Meet us</a></li>
                    <li><a href="">Follow us</a></li>
                </ul>
            </nav>
            </div><!-- end col md 6 menu -->
        </div><!-- end row -->
    </div><!-- end container -->
</div><!-- end header -->

$(document).ready(function(){

    /** 
     * This part does the "fixed navigation after scroll" functionality
     * We use the jQuery function scroll() to recalculate our variables as the 
     * page is scrolled/
     */
    $(window).scroll(function(){
        var window_top = $(window).scrollTop(); // the "12" should equal the margin-top value for nav.stick
        var div_top = $('.header').offset().top;
    });

    /**
     * This part causes smooth scrolling using scrollto.js
     * We target all a tags inside the nav, and apply the scrollto.js to it.
     */
    $(".menu li a").click(function(evn){
        evn.preventDefault();
        $('html,body').scrollTo(this.hash, this.hash);
           if (window_top > div_top) {
                $('nav').addClass('stick');
            } else {
                $('nav').removeClass('stick');
            } 
    });

    /**
     * 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 = $("nav 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];
            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("active");
            } else {
                $("a[href='" + theID + "']").removeClass("active");
            }
        }

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

$(文档).ready(函数(){ /** *此部分提供“滚动后的固定导航”功能 *我们使用jQuery函数scroll()重新计算变量作为 *页面被滚动/ */ $(窗口)。滚动(函数(){ var window_top=$(window.scrollTop();//这个“12”应该等于nav.stick的边距上限值 var div_top=$('.header').offset().top; }); /** *此部分使用scrollto.js实现平滑滚动 *我们以nav中的所有a标记为目标,并对其应用scrollto.js。 */ $(“.menu li a”)。单击(函数(evn){ evn.preventDefault(); $('html,body').scrollTo(this.hash,this.hash); 如果(窗口顶部>分区顶部){ $('nav').addClass('stick'); }否则{ $('nav').removeClass('stick'); } }); /** *此部分处理突出显示功能。 *我们再次使用滚动功能,创建一些数组和 *操作、类添加和类删除以及条件测试 */ var aChildren=$(“nav li”).children();//查找列表项的a子项 var aArray=[];//创建空的aArray 对于(var i=0;i=divPos&&windowPos<(divPos+divHeight)){ $(“a[href=”“+theID+”]”)addClass(“活动”); }否则{ $(“a[href=”“+theID+”]”)removeClass(“活动”); } } 如果(窗口位置+窗口高度==8){ if(!$(“导航李:最后一个孩子a”).hasClass(“活动”)){ var navActiveCurrent=$(“.active”).attr(“href”); $(“a[href='”+navActiveCurrent+“']”)。removeClass(“active”); $(“资产净值:最后一个子a”).addClass(“活动”); } } }); });
window\u top需要是一个全局变量,也可以将指定数量的像素添加到实际变量中,以便
var div\u top=$('.header').offset().top+5

var window\u top
是窗口滚动事件处理程序中的局部变量,以及在
中使用它的原因。菜单li a
单击事件处理程序。这对我来说不太好。将变量设置为全局变量可以解决问题,但简单地用
$(window.scrollTop()
替换
窗口顶部
,也不是一个坏主意。