DEV Community

loading...

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

Areeb ur Rub
18 | coding since 2016 | Arduino | Python | JavaScript | HTML/CSS | Love learn making things
Updated on ・1 min read

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

areeburrub image

Discussion (8)

Collapse
pavelloz profile image
Paweł Kowalski • Edited

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

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

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.