简体   繁体   English

向下滚动时隐藏导航栏,向上滚动时显示

[英]hide nav bar on scroll down and show on scroll up

I want to hide my top nav bar when scrolls down and show when scrolls up like this website nav bar.我想在向下滚动时隐藏我的顶部导航栏,并在像这个网站导航栏一样向上滚动时显示。 currently, I am using this code but it's not working properly, it works when scrolling down but doesn't when scrolling up.目前,我正在使用此代码,但它无法正常工作,它在向下滚动时有效,但在向上滚动时无效。

 <script type="text/javascript"> // Hide #main-nav on on scroll down var didScroll; var lastScrollTop = 0; var delta = 5; var navbarHeight = $('#main-nav').outerHeight(); $(window).scroll(function(event){ didScroll = true; }); setInterval(function() { if (didScroll) { hasScrolled(); didScroll = false; } }, 250); 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.MagicMenu-up. // This is necessary so you never see what is "behind" the navbar. if (st > lastScrollTop && st > navbarHeight){ // Scroll Down $('#main-nav').slideDown(500); } else { // Scroll Up if(st + $(window).height() < $(document).height()) { $('#main-nav').slideUp(500); } } lastScrollTop = st; } </script>

there's no need for setInterval(function() {... as you're executing the function non-stop even when the user is not scrolling, replace it with an eventListener for the scroll,不需要setInterval(function() {...因为即使用户没有滚动,你也会不停地执行 function,将其替换为用于滚动的eventListener

your #main-nav is only sliding up and down, you need to change it's position according to the scroll to make i visible when it slides down,您的#main-nav只是上下滑动,您需要根据滚动条更改它的 position 以使其在向下滑动时可见,

try to avoid jquery animation and use css transition for the effect when possible, it's good for performance,尽量避免 jquery animation 并尽可能使用 css 转换效果,这对性能有好处,

here's an example ( without jQuery ), tweak it to your needs:这是一个示例(没有 jQuery ),根据您的需要进行调整:

 let nav = document.querySelector('#nav'); let lastScrollTop; window.addEventListener("scroll", function() { // listen for the scroll var st = window.pageYOffset || document.documentElement.scrollTop; if (st > lastScrollTop) nav.style.opacity = 0; // hide the nav-bar when going down else nav.style.opacity = 1; // display the nav-bar when going up lastScrollTop = st; nav.style.top = `${st}px`; // set the position of the nav-bar to the current scroll }, false);
 * { box-sizing: border-box; } body { height: 1000px; padding: 0; margin: 0; } #nav { width: 100%; height: 50px; background: blue; position: absolute; transition: opacity 0.5s linear; }
 <div id="nav"> </div>

HTML HTML

<div id="navbar">
    <a href="#home">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
</div>

CSS CSS

#navbar {
  background-color: #333; /* Black background color */
  position: fixed; /* Make it stick/fixed */
  top: 0; /* Stay on top */
  width: 100%; /* Full width */
  transition: top 0.3s; /* Transition effect when sliding down (and up) */
}

/* Style the navbar links */
#navbar a {
  float: left;
  display: block;
  color: white;
  text-align: center;
  padding: 15px;
  text-decoration: none;
}

#navbar a:hover {
  background-color: #ddd;
  color: black;
}

JavaScript JavaScript

When the user scrolls down, hide the navbar .当用户向下滚动时,隐藏navbar When the user scrolls up, show the navbar :当用户向上滚动时,显示navbar

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
    var currentScrollPos = window.pageYOffset;
    if (prevScrollpos > currentScrollPos) {
        document.getElementById("navbar").style.top = "0";
    } else {
        document.getElementById("navbar").style.top = "-50px";
    }
    prevScrollpos = currentScrollPos;
}

I used the JavaScript below for my code since I had a checkbox as a Hamburger Icon that toggles the nav bar when the icon is clicked.我将下面的 JavaScript 用于我的代码,因为我有一个复选框作为汉堡包图标,单击该图标时会切换nav栏。 when on mobile the user presses the hamburger icon, nav bar is displayed, but when he scrolls down, the nav bar is hidden, when he scrolls back up, the nav bar comes back into display:当用户在移动设备上按下汉堡包图标时,会显示nav栏,但当他向下滚动时, nav栏会隐藏,当他向上滚动时, nav栏会重新显示:

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
    var currentScrollPos = window.pageYOffset;
    if (prevScrollpos > currentScrollPos) {
        document.getElementById("check").checked = true;
    } else {
        document.getElementById("check").checked = false;
    }
    prevScrollpos = currentScrollPos;
}

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

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