判断一个touchmove的垂直方向 [英] Determine vertical direction of a touchmove

查看:229
本文介绍了判断一个touchmove的垂直方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现触摸监听器片,以触发取决于它是否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;
});

这篇关于判断一个touchmove的垂直方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆