An Interest In:
Web News this Week
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
February 17, 2022 06:58 am GMT
Original Link: https://dev.to/zainmuhammad/how-to-remove-class-on-scroll-top-and-add-on-scroll-down-on-html-body-using-javascript-4oab
How to remove class on scroll top and add on scroll down on html body using JavaScript.
Here we are going to write simple JavaScript for remove class on html body on scroll top behavior and add class on scroll down behavior which is most used in mobile responsive web designs to show and hide site navbar. We can further hide and show the navbar with some CSS3 transitions (for make attractive) by using with the help of that class which is adding and removing with the help of JavaScript.
JavaScript snippet.
// javascript for remove class on scroll top and add on scroll down var lastScrollTop = 0;var body = document.body;window.addEventListener("scroll",function(){var scrollTop = window.pageYOffset || document.documentElement.scrollTop;if(scrollTop > lastScrollTop){ body.classList.remove("shownavbar");}else{ body.classList.add("shownavbar");}lastScrollTop = scrollTop;});
on Scroll Top Behavior of JavaScript Screenshot
on Scroll Down Behavior of JavaScript Screenshot
Original Link: https://dev.to/zainmuhammad/how-to-remove-class-on-scroll-top-and-add-on-scroll-down-on-html-body-using-javascript-4oab
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To