How to hide a Navigation Menu on Scroll down with CSS and JavaScript

How to hide a Navigation Menu on Scroll down with CSS and JavaScript

Step 1) Add HTML:

Create a navigation bar:

 <div id="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>
Step 2) Add CSS:

Style the navigation bar:

<style>
body {
  margin: 0;
  background-color:  #d6eaf8 ;
  font-family: Arial, Helvetica, sans-serif;
}

#navbar {
  background-color:  #21618c  ;
  position: fixed; /* Make it stick/fixed */
  top: 0; /* Stay on top */
  width: 100%; /* Full width */
  display: block;
  transition: top 0.3s; /* Transition effect when sliding down (and up) */
}
/* Style the navbar links */
#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 15px;
  text-decoration: none;
  font-size: 17px;
}

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

</style>
Step 3) Add JavaScript:
/* When the user scrolls down, hide the navbar. When the user scrolls up, show the 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;
}
Full Code and Output

How to hide a Navigation Menu on Scroll down with CSS and JavaScript
You may Also Like
Scroll to top