-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouchDemo.html
More file actions
58 lines (51 loc) · 1.55 KB
/
touchDemo.html
File metadata and controls
58 lines (51 loc) · 1.55 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
<!DOCTYPE html>
<html style="width: 100%;height: 100%;">
<head>
<title></title>
<!--启用viewport-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style type="text/css">
.spirit {
/* 方块的class名称*/
position: absolute;
width: 50px;
height: 50px;
background-color: black;
color: red;
}
</style>
</head>
<body style="height: 100%;margin:0;padding:0">
<div id="canvas" style="position: relative;width:100%;height: 100%;background-color: darkgrey;">
<div id="spiritID" class="spirit">
</div>
</div>
<script type="text/javascript">
var canvas = document.getElementById('canvas'),
spirit = document.getElementById("spiritID"),
startX, startY;
function touchStart(event) {
event.preventDefault();
if(!event.touches.length) return;
var touch = event.touches[0];
startX = touch.pageX;
startY = touch.pageY;
}
canvas.addEventListener('touchstart', touchStart, false);
function touchMove(event) {
event.preventDefault();
if(!spirit || !event.touches.length) return;
var touch = event.touches[0],
x = touch.pageX - startX,
y = touch.pageY - startY;
spirit.style.webkitTransform = 'translate(' + x + 'px, ' + y + 'px)';
}
canvas.addEventListener('touchmove', touchMove, false);
function touchEnd(event) {
if(!spirit) return;
}
canvas.addEventListener('touchend', touchEnd, false);
</script>
</div>
</body>
</html>