DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Areeb ur Rub
Areeb ur Rub

Posted on • Updated on

Hide NavBar as Scroll down, in less than 10 lines of javascript;

Buy Me A Coffee

You must have seen this effect on several website, where you scroll down and navigation bar automatically hides and reappears when scroll up.

So here's basic page in which I have implemented this,in just 10 lines of javascript.

var lastScrollTop;
navbar = document.getElementById('navbar');
window.addEventListener('scroll',function(){
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
if(scrollTop > lastScrollTop){
navbar.style.top='-80px';
}
else{
navbar.style.top='0';
}
lastScrollTop = scrollTop;
});

How it's working

Here, the position of the navbar is being altered using javascript.

First we create a variable which stores position of Page;

Then we get the scroll position using: window.pageYOffset or for some browser 'document.documentElement.scrollTop';

Then check that weather the page is scrolled up or down;

Then save the scroll vale to the variable;

This is done every time whenever the page is scrolled, as all this lies under a EventListener.

Check the JS in codepen it will make it more clear


Check out my other posts as well

Follow me for more

Top comments (11)

Collapse
pavelloz profile image
PaweΕ‚ Kowalski • Edited on

I think if you removed all your javascript, so its 0 lines of javascript, and removed position:fixed; from css it would hide as well while scrolling. ;)

Collapse
areeburrub profile image
Areeb ur Rub Author

Nice Suggestion, but then it won't appear again when scroll up and you have to reach the Navigation bar back to top.

The Navigation bar is something which should be easily accessible, don't you think after scrolling on a long feed user want NavBar to move to the another page instantly.

Hope you get my point πŸ˜„

Collapse
hasnaindev profile image
Muhammad Hasnain

Sure but if a user has scrolled down and want to access navigation bar, what happens then? Think a little about UX. ;)

Collapse
pavelloz profile image
PaweΕ‚ Kowalski

Im thinking about UX, thats why i propose not using js in this case. ;)

Collapse
jonrandy profile image
Jon Randy • Edited on

I think it needs some refinement - you lose the menu if you scroll down so much as 1 pixel. Similar functionality on websites is more complex than this

Collapse
hasnaindev profile image
Muhammad Hasnain • Edited on

I agree with Areeb. You could write an entire library just to handle the navigation bar and add more complexity but I don't think that's the point. Most of the posts here are written by beginners, for beginners.

Collapse
jonrandy profile image
Jon Randy

Well, you could add one or two lines to stop it disappearing when you scroll as little as one pixel down, which is just plain irritating for the the user

Collapse
areeburrub profile image
Areeb ur Rub Author

Right, it can be conplex but it's the simplest way to do so.

Collapse
vincentshtick profile image
vincent-shtick

You should change the if statement to also include an offset since if the bar disapears immediatly it might look strange ui wise

if(scrollTop > lastScrollTop && scrollTop > your desired offset){

Collapse
blinkeujennie23 profile image
Blinkeu Jennie 2016

Its not working on safari. The navbar jumps to the top when scrolling all the way up

Collapse
aleksejs profile image
Aleksejs Aleksejevs

This is because of safari elastic scrolling/bounce effect, you need to be very precise to scroll it to the top.
Just add
if(scrollTop > lastScrollTop && scrollTop > 10)

And it should be fine

🌚 Life is too short to browse without dark mode