DEV Community

Sreeharsh K
Sreeharsh K

Posted on

Responsive Navbar with Hide on Scroll down and show on scroll up copy paste ready

In this example, we first define a constant variable navbar and a let variable lastScrollPosition to keep track of the user's scroll position. We then add an event listener for the scroll event on the window object.

Inside the event listener, we get the current scroll position using the window.pageYOffset property. We then check if the user has scrolled down or up (as determined by comparing the current scroll position to the previous position stored in the lastScrollPosition variable). If the user has scrolled down, we add the .hide class to the navbar element to hide it. Otherwise, we remove the .hide class to show it. We then update the lastScrollPosition variable for the next event.

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay