-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwipeDetector.js
97 lines (75 loc) · 2.25 KB
/
SwipeDetector.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function SwipeDetect(el, maxSwipe = 50) {
var that = this;
this.elm = el;
this.isSwiping = false;
this.swipeStarted = 0;
this.swipeState = 'none';
this.isSwiped = function() {
return that.swipeState;
}
this.isTouchSurface = function(e) {
return (('ontouchstart' in window) ||
(navigator.maxTouchPoints > 0) ||
(navigator.msMaxTouchPoints > 0));
}
this.mouseDown = function(e) {
e.preventDefault();
that.isSwiping = true;
that.swipeStarted = e.clientX;
}
this.mouseUp = function(e) {
e.preventDefault();
that.isSwiping = false;
return (sw.isSwiped());
}
this.mouseMove = function(e) {
//e.preventDefault();
if (that.isSwiping) {
if (that.swipeStarted < e.clientX) // swiping right side --->
if (e.clientX >= (that.swipeStarted + maxSwipe)) {
that.swipeState = 'right';
return;
}
if (that.swipeStarted > e.clientX){ // swiping left side <---
if (e.clientX <= (that.swipeStarted + maxSwipe)) {
that.swipeState = 'left';
return;
}
}
}
that.swipeState = 'none';
}
this.touchDown = function(e) { //touch start
e.preventDefault();
that.isSwiping = true;
that.swipeStarted = e.changedTouches[0].clientX;
}
this.touchUp = function(e) { //touch end
e.preventDefault();
that.isSwiping = false;
return (sw.isSwiped());
}
this.touchMove = function(e) { //touch move
e.preventDefault();
if (this.isSwiping){
if (that.swipeStarted < e.clientX) // swiping right side --->
if (e.clientX >= (that.swipeStarted + maxSwipe)) {
that.swipeState = 'right';
return;
}
if (that.swipeStarted > e.clientX){ // swiping left side <---
if (e.clientX <= (that.swipeStarted + maxSwipe)) {
that.swipeState = 'left';
return;
}
}
}
that.swipeState = 'none';
}
this.elm.addEventListener('mousedown', this.mouseDown, false);
this.elm.addEventListener('mousemove', this.mouseMove, false);
this.elm.addEventListener('mouseup', this.mouseUp, false);
this.elm.addEventListener('touchstart', this.touchDown, false);
this.elm.addEventListener('touchmove', this.touchMove, false);
this.elm.addEventListener('touchend', this.touchUp, false);
}