jQuery窗口滚动和元素位置问题
我使用 $(window).scroll(function()
在网站导航上设置类。 当某个部分滚动到视图中时,导航类将更改为“当前”。
$(window).scroll(function() {
var top = 0;
top = $(window).scrollTop();
if(top < 1000){
$("a[href='#uk']").parent().addClass("current");
$("a[href='#uk']").parent().siblings().removeClass("current");
}
if((top >= 1000) && (top < 2000)){
$("a[href='#mcr']").parent().addClass("current");
$("a[href='#mcr']").parent().siblings().removeClass("current");
}
if((top >= 2000) && (top < 3000)){
$("a[href='#lpool']").parent().addClass("current");
$("a[href='#lpool']").parent().siblings().removeClass("current");
}
if((top >= 3000) && (top < 4000)){
$("a[href='#bham']").parent().addClass("current");
$("a[href='#bham']").parent().siblings().removeClass("current");
}
});
这很有效,但是当窗口“滚动”到位时(显然)它会起作用。 如果刷新页面,则即使页面保留在某个部分,该类也会被删除。
我如何获取此代码来检查其在页面加载中的位置并立即应用该类?
I'm using the $(window).scroll(function()
to set classes on the navigation of a site.
When a section rolls into view the navigation class changes to 'current'.
$(window).scroll(function() {
var top = 0;
top = $(window).scrollTop();
if(top < 1000){
$("a[href='#uk']").parent().addClass("current");
$("a[href='#uk']").parent().siblings().removeClass("current");
}
if((top >= 1000) && (top < 2000)){
$("a[href='#mcr']").parent().addClass("current");
$("a[href='#mcr']").parent().siblings().removeClass("current");
}
if((top >= 2000) && (top < 3000)){
$("a[href='#lpool']").parent().addClass("current");
$("a[href='#lpool']").parent().siblings().removeClass("current");
}
if((top >= 3000) && (top < 4000)){
$("a[href='#bham']").parent().addClass("current");
$("a[href='#bham']").parent().siblings().removeClass("current");
}
});
This works great, however it works when the window is "scrolled" into place (obviously).
If the page is refreshed then the class is removed even though the page remains at a certain section.
How would I get this code to check where it is on page load and apply the class immediately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)