html
html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML5 Touch Events</title>
</head>
<body>
<div class="box" id="item1"></div>
<div class="box" id="item2"></div>
<div class="box" id="item3"></div>
</body>
</html>
Enter to Rename, Shift+Enter to Preview
css
css
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
html {
box-sizing: border-box;
}
*, ::before, ::after {box-sizing: inherit;}
.box {
position: absolute;
width: 100px;
height: 100px;
}
#item1 {
background-color: teal;
}
#item2 {
background-color: blue;
/*margin-left: 130px;*/
}
#item3 {
background-color: red;
/*margin-left: 250px;*/
}
Enter to Rename, Shift+Enter to Preview
js
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
alert("touch_hold and move boxes around, multi-touch supported!...hold and move all boxes around.")
window.onload = function() {
function initEvent() {
// get target DOM elements
var boxes = document.querySelectorAll(".box");
// loop through array list of draggable DOM elements
for (var i = 0; i < boxes.length; i++) {
var box = boxes[i];
box.addEventListener("touchmove", function(event) {
// align touch event touches with target DOM element
var touch = event.targetTouches[0];
event.target.style.left = touch.pageX - 50 + "px";
event.target.style.top = touch.pageY - 50 + "px";
event.preventDefault();
}, false);
}
}
// start initialization
initEvent();
}
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run