/
touch.html
76 lines (66 loc) · 2.06 KB
/
touch.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<title>Title</title>
</head>
<body>
<style>
.box {
position: absolute;
border: red 1px solid;
}
#mv {
cursor: pointer;
border: blue 1px solid;
}
</style>
<div class="box">
<span id="mv">ddddddddd</span>
<em>ssssssss</em>
</div>
<script>
moveable('mv', null, true);
function moveable(id, endcb, pr) {
var doc = document;
var ele = doc.documentElement;
// 获取节点
var obj = doc.getElementById(id);
var mvobj = pr ? obj.parentElement : obj;
var w, h;
// 绑定touchstart事件
obj.addEventListener("touchstart", function (e) {
var touches = e.touches[0];
w = touches.clientX - mvobj.offsetLeft;
h = touches.clientY - mvobj.offsetTop;
mvobj.classList.add('lamMove');
//阻止页面的滑动默认事件
doc.addEventListener("touchmove", defaultEvent(e), false);
}, false);
obj.addEventListener("touchmove", function (e) {
var touches = e.touches[0];
var left = touches.clientX - w;
var top = touches.clientY - h;
var width = ele.clientWidth - mvobj.offsetWidth;
if (left < 0) {
left = 0;
} else if (left > width) {
left = width
}
mvobj.style.left = left + "px";
mvobj.style.top = top + "px";
}, false);
obj.addEventListener("touchend", function (e) {
doc.removeEventListener("touchmove", defaultEvent(e), false);
mvobj.classList.remove('lamMove');
typeof (endcb) == 'function' && endcb(obj, mvobj);
}, false);
}
function defaultEvent(e) {
e.preventDefault();
}
</script>
</body>
</html>