gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:55:58 25 4
gpt4 key购买 nike

我尝试找到此错误的解决方案,但没有成功,因此我将发布我的具体问题。我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com