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

标签 javascript jquery scroll

我尝试找到此错误的解决方案,但没有成功,因此我将发布我的具体问题。我有一个 jquery 滚动条,可以滚动到菜单中的主题标签,当到达特定的目标主题标签时,它应该将菜单项颜色从灰色更改为白色。一切正常,只有一个小错误。

当您单击菜单项时,它会向下滚动,但您必须再滚动 1-5 像素,以便脚本将菜单项更改为事件状态(白色),控制台还给我一个“无法读取属性”未定义的'top'。我一无所知,因为脚本将菜单项收集在一个数组中,如果我console.log该数组,则项目就在那里。如果我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");
            }
        }
    });
});

最佳答案

window_top 需要是一个全局变量,您也可以将所需的指定像素数量添加到实际变量中,因此 var div_top = $('.header').offset().top + 5;

关于javascript - 无法读取未定义 jquery 滚动的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26218321/

相关文章:

javascript - 创建 ES6 类实例后可以立即将实例绑定(bind)到非静态方法吗?

jquery - 需要修改 JQuery Cycle updateActivePagerLink 选项

javascript - 如何防止用户拖动滚动条移动页面?

javascript - 多元素选择器上的 jQuery 方法链接

javascript - 对 JavaScript 文件的动态引用

javascript - Vue 全局过滤器抛出值未定义的错误

javascript - Ajax(过滤器)加载后加载 jQuery 脚本

php - ajax收到的链接无法点击

javascript - 如何使用 Javascript 或 CSS 悬停按钮并让 div 向右或向左滚动?

ios - 简单滚动