jquery – 确定触摸的垂直方向

前端之家收集整理的这篇文章主要介绍了jquery – 确定触摸的垂直方向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试实现平板电脑的触摸监听器,以触发一些动作,取决于它是否向上或向下触摸。

我尝试了本机侦听器:

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

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

这可能吗?

或者我需要使用touchstart / touchend,如果我需要这个,我可以确定触摸动作停止之前的方向吗?

如果我只能用外部图书馆做这件事,那最好的是什么?

谢谢。

解决方法

您需要保存触摸的最后一个位置,然后将其与当前的位置进行比较。
粗略的例子: @H_301_4@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; });

猜你在找的jQuery相关文章