简体   繁体   English

粘性导航栏,如何在向下滚动时隐藏并在向上滚动时显示

[英]Sticky nav bar, how to hide when scroll down and show when scroll up

I'm trying to hide the sticky nav bar on scroll down and show it again whilst the screen is being scroll up.我试图在向下滚动时隐藏粘性导航栏,并在屏幕向上滚动时再次显示它。 At the moment the sticky nav bar is still operating as normal without this affect.目前,粘性导航栏仍然正常运行,没有这种影响。

Not sure what I need to do to fix it:不知道我需要做什么来修复它:

HTML: HTML:

<header id="site-header" class="header-footer-group _mPS2id-t mPS2id-target mPS2id-target-first mPS2id-target-last" role="banner">
    
</header>

CSS: CSS:

#site-header{
    opacity: 0.9;
    width:100% !important;
    z-index:99999;
  position: sticky;
    position: -webkit-sticky;
  top: 0;
    height:166px;
}

.navup{
    transform: translatey(-166px);
}

Javascript: Javascript:

<script>
var my_window = window;
var position = my_window.scrollTop;
    
my_window.scroll(function () {
    if (my_window.scrollTop  > position) {
        $('#site-header').addClass('navup');
    }else{
        $('#site-header').removeClass('navup');
    }
    
    position = my_window.scrollTop;
});
    
</script>

In first line of script, you should create a selector of window object, like: $(window) .在脚本的第一行,您应该创建一个选择器 window object,例如: $(window) After that, add parenthesis after every scrollTop occurence, like this scrollTop() since it is a method, not a property.之后,在每个scrollTop出现后添加括号,就像这样scrollTop()因为它是一个方法,而不是一个属性。

This should do it:这应该这样做:

var my_window = $(window);
var position = my_window.scrollTop();
    
my_window.scroll(function () {
    if (my_window.scrollTop()  > position) {
        $('#site-header').addClass('navup');
    }else{
        $('#site-header').removeClass('navup');
    }
    
    position = my_window.scrollTop();
});

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

相关问题 仅在移动设备上时,向下滚动时隐藏导航,向上滚动时显示 - Hide nav on scroll down and show on scroll up only when in mobile 向下滚动时隐藏导航栏并在向上滚动时显示它 - Hide nav bar on scroll down and show it on scroll up 用户向上/向下滚动时如何隐藏/显示导航栏 - How to hide/show nav bar when user scrolls up/down 向下滚动或向上滚动时如何显示或隐藏菜单? - How to show or hide a menu when I scroll down or up? 如何在向下滚动时隐藏div,然后向上滚动显示 - How to hide div when scrolling down and then show scroll up 向下滚动以显示标题并在向上滚动时隐藏 - Scroll down to show header and hide when scroll up 当滚动条下降时如何显示一个div,当我滚动条上升时它应该消失 - how to show a div when scroll bar goes down, and when i scrollbar goes up it should disappear 使用香草JavaScript向下滚动时隐藏菜单,向上滚动时显示 - Hide menu when scroll down and show when scroll up by using vanilla JavaScript 当我向上滚动并在向上滚动或当我将鼠标悬停在上面时,如何使导航条变得透明? - How do i make my nav bar transparent when I scroll down and come back when I scroll up or when I hover my mouse over is? 向下滚动时隐藏菜单并在向上滚动时显示 - Hide menu on scroll down and show on scroll up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM