-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-swipe滑动事件.html
81 lines (71 loc) · 1.93 KB
/
08-swipe滑动事件.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no">
<title>08-swipe滑动事件.html</title>
<style>
div:nth-child(1) {
width: 200px;
height: 200px;
background-color: aqua;
margin: 100px auto;
}
</style>
</head>
<body>
<div></div>
<script>
/*
1 手指的个数 不能超过 1
2 手指滑动的距离不能 短 15px
3 手指按下的时间不能太长 500ms
*/
var div = document.querySelector("div");
// 记录手指按下的坐标
var startX, startY;
// 记录手指按下的时间
var startTime;
div.addEventListener("touchstart", function (e) {
// 判断手指的个数
if (e.targetTouches.length > 1) {
return;
}
// 记录手指按下的坐标
startX = e.targetTouches[0].clientX;
startY = e.targetTouches[0].clientY;
// 记录手指按下的时间
startTime = Date.now();
});
div.addEventListener("touchend", function (e) {
// 判断手指的个数
if (e.changedTouches.length > 1) {
return;
}
// 获取手指松开的坐标
var endX = e.changedTouches[0].clientX;
var endY = e.changedTouches[0].clientY;
// 获取手指松开的时间
var endTime = Date.now();
// 方向
var direction;
// 判断距离 水平距离
if (Math.abs(endX - startX) > 5) {
// 判断方向 水平
direction = endX > startX ? "right" : "left";
} else if (Math.abs(endY - startY) > 5) {
// 判断方向 竖直方向
direction = endY > startY ? "down" : "up";
} else {
return;
}
// 判断时间
if (endTime - startTime > 500) {
return;
}
// 执行自己的逻辑
console.log(direction);
});
</script>
</body>
</html>