手机上的滚动方向

Scroll direction on phone?

本文关键字:方向 滚动 手机      更新时间:2023-09-26

如何检测手机上的滚动方向?我目前正在使用

$window.on( 'touchmove', onPhone );

在触摸滚动发生时调用onPhone函数。但我如何检测它是向下还是向上滚动呢?

提前感谢!

我想你只需要上下滚动。

var lastPoint = null; //global
$(window).on('touchend', function(e){
    var currentPoint = e.originalEvent.changedTouches[0].pageY;
    if(lastPoint != null && lastPoint < currentPoint ){
        //swiped down
        console.log('you scrolled up');
    }else if(lastPoint != null && lastPoint > currentPoint){
        //swiped up
        console.log('you scrolled down');
    }
    lastPoint = currentPoint;
});

如果你想检测左右滚动方向可以使用相同的代码,但将'pageY'更改为'pageX'。

希望这对你有帮助!