判断一个touchmove的垂直方向方向、touchmove

2023-09-06 02:10:57 作者:迷失自我

我试图实现触摸监听器片,以触发取决于它是否touchmoved向上或向下的一些动作。

i'm trying to implement a touch listener for tablets to trigger some actions depending whether it touchmoved upwards or downwards.

我尝试了本地监听器:

($document).bind('touchmove', function (e)
{
    alert("it worked but i don't know the direction");
});

但我不知道如何确定的方向。

But i don't know how to determine the direction.

这可能吗?

或者我需要使用touchstart / touchend,如果我需要这个我能确定方向的触摸运动停止前?

Or do i need to use touchstart/touchend, if I need this can I determine the direction before the touch movement stops?

如果我只能做到这一点与外部库,什么是最好的?

If I can only do this with an external library, what's the best one?

感谢。

推荐答案

您需要保存触摸的最后一个位置,然后把它比作当前之一。 粗例如:

You need to save the last position of the touch, then compare it to the current one. Rough example:

var lastY;
$(document).bind('touchmove', function (e){
     var currentY = e.originalEvent.touches[0].clientY;
     if(currentY > lastY){
         // moved down
     }else if(currentY < lastY){
         // moved up
     }
     lastY = currentY;
});