检测滚动方向
利用JavaScript的滚动事件,无需jQuery即可确定滚动方向。
检测滚动方向
为了实现这一点,我们将存储先前的scrollTop值并将其与当前的scrollTop值进行比较。
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 使用该方法,无需依赖第三方库,即可准确检测任意网页的滚动方向。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3