Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 17, 2022 06:58 am GMT

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 Top Behavior of JavaScript Screenshot

on Scroll Down 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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To