-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch.js
75 lines (72 loc) · 1.93 KB
/
touch.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
/*
https://github.com/xd-tayde/mtouch/blob/master/README_ZH.md
*/
function touch(ele, option) {
ele.addEventListener("touchstart", function(e) {
this.startX = e.touches[0].pageX
this.startY = e.touches[0].pageY
})
ele.addEventListener("touchmove", function(e) {
this.isMove = true;
this.endX = e.touches[0].pageX
this.endY = e.touches[0].pageY
this.distanceX = this.endX - this.startX
this.distanceY = this.endY - this.startY
})
ele.addEventListener("touchend", function() {
if (this.isMove) {
this.isMove = false
//如果滑动距离太短
if (Math.abs(this.distanceX) < 2 && Math.abs(this.distanceY) < 2) {
return false;
}
let angle = Math.atan2(this.distanceY, this.distanceX) * 180 / Math.PI
const EVENT = {
self: this,
distanceX: this.distanceX || 0,
distanceY: this.distanceY || 0,
startX: this.startX,
startY: this.startY,
endX: this.endX,
endY: this.endY
}
if (angle >= -45 && angle < 45) {
// console.log('向右')
option.right && option.right(EVENT)
} else if (angle >= 45 && angle < 135) {
// console.log('向下')
option.down && option.down(EVENT)
} else if (angle >= -135 && angle < -45) {
// console.log('向s上')
option.up && option.up(EVENT)
} else if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
// console.log('向左')
option.left && option.left(EVENT)
}
}
})
}
function safelyResolveThen(self, then) {
let called = false
try {
then(function(value) {
if (called) {
return
}
called = true
doResolve(self, value)
}, function(error) {
if (called) {
return
}
called = true
doReject(self, error)
})
} catch (error) {
if (called) {
return
}
called = true
doReject(self, error)
}
}