-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswipe.js
More file actions
113 lines (83 loc) · 2.96 KB
/
swipe.js
File metadata and controls
113 lines (83 loc) · 2.96 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Hey there!,
I am Ezekiel Saturday
Director of the Wickend Initiatives,
Software developer, swim coach and Teacher.
the Wickend Initiatives is a non-profit NGO dedicated to doing the hardwork of creating more opportunity
and possibilities for the world.
While I was creating a webprogram with the need to detect a swipe movement on an element,
I had created this function which seems to work just fine to suite that functionality.
and now I am sharing it here with you for your own personal benefits.
querySelectors are used by including tagName or full class names like the querySelectorAll method
The querySelectorAll method, which is defined both on the document object and on element nodes, takes a selector string and returns an arraylike object containing all the elements that it matches.
<p > And if you go chasing
< span class =" animal " > rabbits </ span >
</p >
<p >
And you know you ' re going to fall
</ p >
<p >
Tell ' em a < span class =" character " > hookah smoking
< span class =" animal " > caterpillar </ span > </ span >
</p >
<p >
Has given you the call
</ p >
< script >
function count ( selector ) {
return document . querySelectorAll ( selector ) . length ;
}
console . log ( count (" p ") ) ; // All <p > elements
// → 4
console . log ( count (". animal ") ) ; // Class animal
// → 2
console . log ( count (" p . animal ") ) ; // Animal inside of <p >
// → 2
console . log ( count (" p > . animal ") ) ; // Direct child of <p >
// → 1
</ script >
*/
var container = document.querySelector("TAGNAME, CLASS OR ID FOR WHERE YOU WANT TO DETECT SWIPE");
container.addEventListener("touchstart", startTouch, false);
container.addEventListener("touchmove", moveTouch, false);
// Swipe Up / Down / Left / Right
var initialX = null;
var initialY = null;
function startTouch(e) {
initialX = e.touches[0].clientX;
initialY = e.touches[0].clientY;
};
function moveTouch(e) {
if (initialX === null) {
return;
}
if (initialY === null) {
return;
}
var currentX = e.touches[0].clientX;
var currentY = e.touches[0].clientY;
var diffX = initialX - currentX;
var diffY = initialY - currentY;
if (Math.abs(diffX) > Math.abs(diffY)) {
// sliding horizontally
if (diffX > 0) {
// swiped left
console.log("swiped left");
} else {
// swiped right
console.log("swiped right");
}
} else {
// sliding vertically
if (diffY > 0) {
// swiped up
console.log("swiped up");
} else {
// swiped down
console.log("swiped down");
}
}
initialX = null;
initialY = null;
e.preventDefault();
};