22
Reveal navbar on scroll
After I watched that, I thought of recreating it and here I'm showing it to you all.
I'm sure you can write some markup and a lots of the lorem text. So we'll jump right to my javascript logic.
- window.pageYOffset : It basically returns the position of scrollbar. Check out More on pageYOffset
- Basic JavaScript
- Basic Logical Resoning
- Some time
When the user scroll's down the navbar must hide itself and when the user starts to scroll up it must reveal itself back.
So let us have some variables,
- lastScroll - Stores the just previous scroll position
- currentScroll - Stores the currenrt scroll position
If differnce between currentScroll and lastScroll is positive, it means that user has -- SCROLLED DOWN -- and therefore we will hide the navbar.
Similarly, If differnce between currentScroll and lastScroll is negative, it means that user has -- SCROLLED UP -- and therefore we will reveal the navbar
We will also need some css classes to help us perform the magic,
- .scroll-up - what happens after user scrolls up
- .scroll-down - what happens after user scrolls down
.scroll-up
header.scroll-up{
transform: translateY(0);
}
.scroll-down
header.scroll-down{
transform: translateY(-100%);
}
amdhanwate / reveal-unreveal-on-scroll
Reveal and unreveal the navbar depending upon user's scroll behavior
Do tell me your thoughts on this and I welcome all type of criticism (I don't know the spelling) which would help me become a better developer after all.
Also, a BIG thanks to our developer community for always helping and encouraging everyone around.
I hope you wait for my next article..
22