Detecting Scroll Direction
Utilizing JavaScript's scroll event, it's possible to determine the direction of scrolling without the need for jQuery.
Detecting Scroll Direction
To accomplish this, we'll store the previous scrollTop value and compare it to the current scrollTop value.
var lastScrollTop = 0;
// element should be replaced with the actual target element on which you have applied scroll, use window in case of no target element.
element.addEventListener("scroll", function () { // or window.addEventListener("scroll"....
var st = window.pageYOffset || document.documentElement.scrollTop; // Credits: "https://github.com/qeremy/so/blob/master/so.dom.js#L426"
if (st > lastScrollTop) {
// downscroll code
} else if (st By employing this method, you can accurately detect the scrolling direction in any web page without relying on third-party libraries.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3