可以使用Window.Onscroll方法来包含滚动方向的检测吗? [英] Can one use Window.Onscroll method to include detection of scroll direction?
本文介绍了可以使用Window.Onscroll方法来包含滚动方向的检测吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以使用Window.Onscroll方法来检测滚动方向吗?
Can one use Window.Onscroll method to include detection of scroll direction?
推荐答案
如果你录制了scrollX并且滚动了页面加载和每次滚动事件发生时,您可以将以前的值与新值进行比较,以了解您滚动的方向。这是一个概念证明:
If you record the scrollX and scrollY on page load and each time a scroll event occurs, then you can compare the previous values with the new values to know which direction you scrolled. Here's a proof of concept:
function scrollFunc(e) {
if ( typeof scrollFunc.x == 'undefined' ) {
scrollFunc.x=window.pageXOffset;
scrollFunc.y=window.pageYOffset;
}
var diffX=scrollFunc.x-window.pageXOffset;
var diffY=scrollFunc.y-window.pageYOffset;
if( diffX<0 ) {
// Scroll right
} else if( diffX>0 ) {
// Scroll left
} else if( diffY<0 ) {
// Scroll down
} else if( diffY>0 ) {
// Scroll up
} else {
// First scroll event
}
scrollFunc.x=window.pageXOffset;
scrollFunc.y=window.pageYOffset;
}
window.onscroll=scrollFunc
这篇关于可以使用Window.Onscroll方法来包含滚动方向的检测吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文