元素拖放

 1             // 获取节点
 2             var block = document.getElementById("right");
 3             var oW, oH;
 4             // 绑定touchstart事件
 5             block.addEventListener("touchstart", function (e) {
 6                 var touches = e.touches[0];
 7                 oW = touches.clientX - block.offsetLeft;
 8                 oH = touches.clientY - block.offsetTop;
 9                 //阻止页面的滑动默认事件
10                 document.addEventListener("touchmove", defaultEvent(event), false);
11             }, false);
12             block.addEventListener("touchmove", function (e) {
13                 var touches = e.touches[0];
14                 var oLeft = touches.clientX - oW;
15                 var oTop = touches.clientY - oH;
16                 if (oLeft < 0) {
17                     oLeft = 0;
18                 } else if (oLeft > document.documentElement.clientWidth - block.offsetWidth) {
19                     oLeft = (document.documentElement.clientWidth - block.offsetWidth);
20                 }
21                 block.style.left = oLeft + "px";
22                 block.style.top = oTop + "px";
23             }, false);
24             block.addEventListener("touchend", function () {
25                 document.removeEventListener("touchmove", defaultEvent(event), false);
26             }, false);
27             function defaultEvent(event) {
28                 event.preventDefault();
29             };

 

posted @ 2018-03-28 15:27  =DLSS=webZT  阅读(169)  评论(0编辑  收藏  举报