Detect swipe gesture using javascript

拜拜、爱过 提交于 2019-12-23 05:29:16

问题


Hi I want to know how I can detect a swipe gesture using javascript. I dont want to use a separate library for this since I have the event object from my javascript callback at my disposal, which has everything I need such as deltaX, deltaY & absDeltaX & absDeltaY. My problem is what bounds do I need to set so that I can differentiate a swipe gesture from other gestures such as scrolling.

This is a problem because sometimes when a user tries to swipe across the screen they also create a significant Y displacement which leads to the gesture also being detected as a scroll when it is only swipe.


回答1:


I would see this as a judgement call, what feels usable? I would investigate the algorithms used by standard libraries. For example we see in the jQuery documentation the rules for a gesture being interpreted as a swipe:

when a horizontal drag of 30px or more (and less than 75px vertically) occurs within 1 second duration:

And jQuery allows those parameters to be configured, implying that there's no, single, "correct" answer.



来源:https://stackoverflow.com/questions/19419131/detect-swipe-gesture-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!