简体   繁体   English

修复了页面重新加载后顶部导航消失的问题

[英]Fixed top navigation disappears after page reload

I am using Chrome, and when i reload my page it brings me to the point of the page i was before reloading. 我使用的是Chrome,当我重新加载页面时,它使我回到了重新加载页面之前的位置。 So when i already scrolled half of the page and i reload it, chrome brings me back to where i scrolled to before. 因此,当我已经滚动了一半页面并重新加载页面时,chrome使我回到了以前滚动到的位置。

I got a fixed nav-bar on my page: 我在页面上有一个固定的导航栏:

$(document).ready(function() {  
var  mn = $(".top-menu");
mns = "main-nav-scrolled";
hdr = $('header').height();

$(window).scroll(function() {
if( $(this).scrollTop() > hdr) {
mn.addClass(mns);
} else {
mn.removeClass(mns);
}
});
});

The navigation works, but when i reload the page it is dissapeared until i start scrolling again. 导航有效,但是当我重新加载页面时,它消失了,直到我再次开始滚动。 Someone has any idea how to fix this? 有人知道如何解决此问题吗?

Try something like that : 试试这样的事情:

$(document).ready(function() {  
    $(window).scroll(sticky_nav());
    sticky_nav(); // Force the first call on refresh 
});

function sticky_nav() {
    var  mn = $(".top-menu");
    mns = "main-nav-scrolled";
    hdr = $('header').height();

    if( $(this).scrollTop() > hdr) {
        mn.addClass(mns);
        } else {
        mn.removeClass(mns);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM