16 function drag(event
) {
17 d
= document
.getElementById('drag_div');
18 offsetX
= event
.clientX
- d
.offsetLeft
;
19 offsetY
= event
.clientY
- d
.offsetTop
;
22 function drop(event
) {
23 d
= document
.getElementById('drag_div');
24 d
.style
.left
= event
.clientX
- offsetX
;
25 d
.style
.top
= event
.clientY
- offsetY
;
28 function touchStart(event
) {
29 d
= document
.getElementById('drag_div');
30 offsetX
= event
.touches
[0].clientX
- d
.offsetLeft
;
31 offsetY
= event
.touches
[0].clientY
- d
.offsetTop
;
34 function touchEnd(event
) {
35 d
= document
.getElementById('drag_div');
36 d
.style
.left
= event
.changedTouches
[0].clientX
- offsetX
;
37 d
.style
.top
= event
.changedTouches
[0].clientY
- offsetY
;
44 <div id=
"drag_div"> </div>
47 document
.getElementById('body').addEventListener('mouseup', drop
);
48 document
.getElementById('body').addEventListener('touchend', touchEnd
);
49 document
.getElementById('drag_div').addEventListener('mousedown', drag
);
50 document
.getElementById('drag_div').addEventListener('touchstart', touchStart
);