简体   繁体   English

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

[英]Hide menu on scroll down and show on scroll up

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 :脚本:

$(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 : 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

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

I tested it and it works fine!!我测试了它,它工作正常!!

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

Add an else to your scrollTop with a removeClass and you should be fine, I tested it and it works.使用 removeClass 将 else 添加到您的 scrollTop 中,您应该没问题,我对其进行了测试并且可以正常工作。 Here这里

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

Detect nav direction with a variable使用变量检测导航方向

var lastscrolltop=0;
jQuery(window).bind('scroll', function () {
        if (jQuery(window).scrollTop() > lastscrolltop)
            jQuery('.mainmenu').addClass('nav-up');
        else
            jQuery('.mainmenu').removeClass('nav-up');
        lastscrolltop=jQuery(window).scrollTop();
});

and use css transition for a smooth show/hide并使用 css 过渡进行平滑的显示/隐藏

.mainmenu {
    transition:all 0.5s ;
}

Your way is too much complicated.你的方法太复杂了。 You can hide the menu on scroll with a simple transition using jQuery .fadeIn() and fadeOut() , without the need for css.您可以使用 jQuery .fadeIn()fadeOut()通过简单的过渡隐藏滚动菜单,而无需 css。

var lastScrollTop = 0;

$(window).scroll(function(event){

   var st = $(this).scrollTop();

   if (st > lastScrollTop){
      $('.mainmenu').fadeOut('fast');
   } else {
      $('.mainmenu').fadeIn('fast');
   }

lastScrollTop = st;

});

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

相关问题 向下滚动时隐藏菜单在向上滚动时显示 - Safari 的问题 - Hide Menu on scroll down show on scroll up - issue with Safari 向下滚动或向上滚动时如何显示或隐藏菜单? - How to show or hide a menu when I scroll down or up? 在向下滚动时隐藏菜单,然后在向上滚动达到0时显示菜单[javascript]对我不起作用 - hide menu on scroll down then show the menu when the scroll up reach 0 [javascript] not working for me 如何在向下滚动时隐藏标题,在向上滚动时显示,如linkedin菜单 - How to hide header on scroll down, show on scroll up like linkedin menu 我如何在向下滚动时创建隐藏并在向上滚动菜单上显示隐藏的jQuery或其他依赖项 - How can I create a hide on scroll down and show on scroll up menu whitout jquery or other dependencies 向下滚动时隐藏菜单,向上滚动时显示,适用于 Chrome,不适用于 Safari(移动) - Hide Menu on Scroll Down, Show on Scroll Up, Works in Chrome, Not in Safari (mobile) 使用香草JavaScript向下滚动时隐藏菜单,向上滚动时显示 - Hide menu when scroll down and show when scroll up by using vanilla JavaScript 上下滚动时显示和隐藏按钮 - show and hide button on scroll up and down 上下滚动显示和隐藏Div - Show and hide a Div on scroll up and down 向下滚动以显示标题并在向上滚动时隐藏 - Scroll down to show header and hide when scroll up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM