向下滚动隐藏菜单并向上滚动显示

时间:2023-01-16 12:34:04

I made this snippet code to hide menu on scroll down and show on scroll up but I have some issues, when I scroll to top the menu still have fixed position, how I can resolve this problem, Thanks!
JAVSCRIPT :

我做了这个片段代码隐藏菜单向下滚动并显示向上滚动但我有一些问题,当我滚动到顶部菜单仍然有固定的位置,我如何解决这个问题,谢谢! JAVSCRIPT:

$(window).bind('scroll', function () {
    if ($(window).scrollTop() > 500) {
        $('.mainmenu').addClass('nav-down');
    }
});

// Hide Header on on scroll down
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('.mainmenu').outerHeight();

$(window).scroll(function(event){
    didScroll = true;
});

setInterval(function() {
    if (didScroll) {
        hasScrolled();
        didScroll = false;
    }
}, 500);

function hasScrolled() {
    var st = $(this).scrollTop();

    // Make sure they scroll more than delta
    if(Math.abs(lastScrollTop - st) <= delta)
        return;

    // If they scrolled down and are past the navbar, add class .nav-up.
    // This is necessary so you never see what is "behind" the navbar.
    if (st > lastScrollTop && st > navbarHeight){
        // Scroll Down
        $('.mainmenu').removeClass('nav-down').addClass('nav-up');
    } else {
        // Scroll Up
        if(st + $(window).height() < $(document).height()) {
            $('.mainmenu').removeClass('nav-up');
        }
    }

    lastScrollTop = st;
}

CSS :

.mainmenu {
    background: #222;
    height: 50px;
    padding: 0 15px;
    width: 80%;
    margin: 0 auto;
}
.nav-down{
    position: fixed;
    top: 0;
    transition: top 0.2s ease-in-out;
    width: 100%;
}
.nav-up {
    top: -50px;
}

Demo : jsfiddle

演示:jsfiddle

2 个解决方案

#1


2  

To you first listener, just add an else statement as follows:

对于第一个侦听器,只需添加一个else语句,如下所示:

$(window).bind('scroll', function () {

    if ($(window).scrollTop() > 150)
        $('.mainmenu').addClass('nav-down');
    else
        $('.mainmenu').removeClass('nav-down');
});

Also note that you don't need a setInterval() for the second listener, see jsfiddle

另请注意,第二个侦听器不需要setInterval(),请参阅jsfiddle

#2


0  

Add an else to your scrollTop with a removeClass and you should be fine, I tested it and it works. Here

使用removeClass为你的scrollTop添加一个else,你应该没问题,我测试了它,它的工作原理。这里

$(window).bind('scroll', function () {
if ($(window).scrollTop() > 500) {
    $('.mainmenu').addClass('nav-down');
}
else
{
    $('.mainmenu').removeClass('nav-down');
}
});

#1


2  

To you first listener, just add an else statement as follows:

对于第一个侦听器,只需添加一个else语句,如下所示:

$(window).bind('scroll', function () {

    if ($(window).scrollTop() > 150)
        $('.mainmenu').addClass('nav-down');
    else
        $('.mainmenu').removeClass('nav-down');
});

Also note that you don't need a setInterval() for the second listener, see jsfiddle

另请注意,第二个侦听器不需要setInterval(),请参阅jsfiddle

#2


0  

Add an else to your scrollTop with a removeClass and you should be fine, I tested it and it works. Here

使用removeClass为你的scrollTop添加一个else,你应该没问题,我测试了它,它的工作原理。这里

$(window).bind('scroll', function () {
if ($(window).scrollTop() > 500) {
    $('.mainmenu').addClass('nav-down');
}
else
{
    $('.mainmenu').removeClass('nav-down');
}
});