-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwipeEvents.js
More file actions
52 lines (45 loc) · 1.42 KB
/
SwipeEvents.js
File metadata and controls
52 lines (45 loc) · 1.42 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
export default class SwipeEvents {
constructor (el) {
this.startX = 0
this.startY = 0
this.el = el
this.el.addEventListener('touchstart', this.touchstart.bind(this), { passive: true })
}
removeEventListener () {
this.el.removeEventListener('touchstart', this.touchstart.bind(this), { passive: true })
}
touchstart (event) {
const touches = event.touches
if (touches && touches.length) {
this.startX = touches[0].pageX
this.startY = touches[0].pageY
this.el.addEventListener('touchmove', this.touchmove.bind(this), { passive: true })
}
}
touchmove (event) {
const touches = event.touches
if (touches && touches.length) {
const deltaX = this.startX - touches[0].pageX
const deltaY = this.startY - touches[0].pageY
if (deltaX >= 50) {
const event = new Event('swipeLeft')
this.el.dispatchEvent(event)
}
if (deltaX <= -50) {
const event = new Event('swipeRight')
this.el.dispatchEvent(event)
}
if (deltaY >= 50) {
const event = new Event('swipeUp')
this.el.dispatchEvent(event)
}
if (deltaY <= -50) {
const event = new Event('swipeDown')
this.el.dispatchEvent(event)
}
if (Math.abs(deltaX) >= 50 || Math.abs(deltaY) >= 50) {
this.el.removeEventListener('touchmove', () => { this.touchmove }, { passive: true })
}
}
}
}