Masud Rana
Masud Rana

Reputation: 630

How To Hide Navbar or any others section on Scroll Down in React or JavaScript?

How To Hide Navbar or any others section on Scroll Down in React or JavaScript ?

I'm try to solve by using this code you can try to use it

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;
}
body {
  margin: 0;
  background-color: #f1f1f1;
  font-family: Arial, Helvetica, sans-serif;
}

#navbar {
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
  transition: top 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

#navbar a {
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 15px;
  text-decoration: none;
  font-size: 17px;
}

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


### javaScript
<div id="navbar">
  <a href="#home">Home</a>
  <a href="#news">About</a>
  <a href="#contact">Services</a>
  <a href="#contact">FAQ</a>
</div>

<div style="padding:15px 15px 2000px;font-size:30px;margin-top:30px;">
  <p><b>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit</b></p>
  <p>Scroll up to show the navbar.</p>
  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>




### style

This is for React

If you want to use This in React try to use this

like this

    render() {

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


        return (
            <header className="header gradientBg1" id="header" style={{
                transition: "top 0.3s", position: "sticky", zIndex: "1"}}>
Your navitems
           </header>
);
}

getting id "header" it add when you scroll down style top: "-55px"; and remove style when you scroll top

NB: Please Help me if you have better solution

Upvotes: 0

Views: 384

Answers (1)

shahabvshahabi
shahabvshahabi

Reputation: 955

The better solution is using wheel event like so :

document.addEventListener("wheel", function(event){

   if(event.deltaY > 0) {
     // we are going down
     document.getElementById("header").style.top = "-55px";

   }

   else {
     // we are going up
    document.getElementById("header").style.top = "0";

   }
})

Upvotes: 1

Related Questions