Show Hide Navbar on Scroll JavaScript

Show Hide Navbar on Scroll JavaScript
Code Snippet:Pure JavaScript: show/hide element on scroll
Author: Lou Stringer
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 6,078
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create a show hide navbar functionality on scroll event. It detects the page scrolling position and applies show/hide rules accordingly. The navbar hides on scroll down and shows on scroll up.

The navbar comes with a simple and clean design in horizontal layout. You can customize the navigation menu according to your needs. Similarly, you can also integrate this show/hide feature to your existing navbar.

How to Create Show Hide Navbar on Scroll JavaScript

1. First of all, create the nav element in HTML and place your links inside it. You can skip this step in case you have already a nav element in your webpage.

  <nav>
    <a href="" class="home">Portfolio</a>
    <a href="#about">about</a>
    <a href="#projects">projects</a>
    <a href="#blog">blog</a>
    <a href="#contact">contact</a>
  </nav>

2. After that, add the following CSS styles to your project. You can set the custom background color, font family, and font size in order to customize the navbar.

If you want to apply show/hide feature to your existing navbar, then you must need to define its fixed position with 0 top left values. Likewise, you need to define a class name “nav-up” with -70px top value.

@font-face {
  font-family: 'Pacifico';
  font-style: normal;
  font-weight: 400;
  src: local('Pacifico Regular'), local('Pacifico-Regular'), url(https://fonts.gstatic.com/s/pacifico/v12/FwZY7-Qmy14u9lezJ-6H6Mk.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

* {
  margin: 0;
  padding: 0;
}

main {
  height: 200vh;
}
header{
    margin-top: 70px;
}

nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 70px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: #e7e7e7;
  transition: all 0.3s;
}

nav a {
  width: 90px;
  margin-right: 10px;
  font-family: Pacifico;
  font-size: 1.5em;
  text-align: center;
  text-decoration: none;
  color: #404040
}

a.home {
  width: 200px;
  margin-right: auto;
  font-size: 2em;
}

.nav-up {
  top: -70px;
}

3. Finally, add the following JavaScript code between the <script> and </script> before closing the body tag. The navHeight variable is necessary to count the visibility and invisibility according to the height of the navigation. Update its value according to the height of your navbar.

const nav = document.querySelector("nav");
const navHeight = 70;
// the point the scroll starts from (in px)
let lastScrollY = 0;
// how far to scroll (in px) before triggering
const delta = 10;

// function to run on scrolling
function scrolled() {
  let sy = window.scrollY;
  // only trigger if scrolled more than delta
  if (Math.abs(lastScrollY - sy) > delta) {
    // scroll down -> hide nav bar
    if (sy > lastScrollY && sy > navHeight) {
      nav.classList.add("nav-up");
    } 
    // scroll up -> show nav bar
    else if (sy < lastScrollY) {
      nav.classList.remove("nav-up");
    }
   // update current scroll point
   lastScrollY = sy 
  }
}

// Add event listener & debounce so not constantly checking for scroll
let didScroll = false;
window.addEventListener("scroll", function(e){
  didScroll = true;
});

setInterval(function() {
  if (didScroll) {
    scrolled();
    didScroll = false;
   }
}, 250)

That’s all! hopefully, you have successfully integrated this show hide navbar code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X