1document.addEventListener('touchstart', handleTouchStart, false);
2document.addEventListener('touchmove', handleTouchMove, false);
3
4var xDown = null;
5var yDown = null;
6
7function getTouches(evt) {
8 return evt.touches || // browser API
9 evt.originalEvent.touches; // jQuery
10}
11
12function handleTouchStart(evt) {
13 const firstTouch = getTouches(evt)[0];
14 xDown = firstTouch.clientX;
15 yDown = firstTouch.clientY;
16};
17
18function handleTouchMove(evt) {
19 if ( ! xDown || ! yDown ) {
20 return;
21 }
22
23 var xUp = evt.touches[0].clientX;
24 var yUp = evt.touches[0].clientY;
25
26 var xDiff = xDown - xUp;
27 var yDiff = yDown - yUp;
28
29 if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
30 if ( xDiff > 0 ) {
31 /* left swipe */
32 } else {
33 /* right swipe */
34 }
35 } else {
36 if ( yDiff > 0 ) {
37 /* up swipe */
38 } else {
39 /* down swipe */
40 }
41 }
42 /* reset values */
43 xDown = null;
44 yDown = null;
45};