问题
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