-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle-touch-gestures.js
48 lines (41 loc) · 1.43 KB
/
handle-touch-gestures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* About: function to handle touch direction in browser
Usage: custom event: custom_handleTouch:'direction' (direction = left, right, up, down)
Example: $(document).on('custom_handleTouch:left', function() { // own code });
*/
function handleTouchGestures() {
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
var xDown, yDown = null;
function handleTouchStart(event) {
xDown = event.touches[0].clientX;
yDown = event.touches[0].clientY;
}
function handleTouchMove(event) {
if (!xDown || !yDown) {
return;
}
var xUp = event.touches[0].clientX;
var yUp = event.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if (Math.abs(xDiff) > Math.abs(yDiff) ) {
if (xDiff > 0) {
// left swipe
$(document).trigger('custom_handleTouch:left');
} else {
// right swipe
$(document).trigger('custom_handleTouch:right');
}
} else {
if (yDiff > 0) {
// up swipe
$(document).trigger('custom_handleTouch:up');
} else {
// down swipe
$(document).trigger('custom_handleTouch:down');
}
}
// reset values
xDown = yDown = null;
}
}