#div1 {Width:100px;height:100px;background:red;position:absolute;}
Html
<div id= "Div1" ></div>
Js
1Window.onload =function () {2 varOdiv = document.getElementById (' Div1 '));3 //PC-Side4Odiv.onmousedown =function(EV) {5 varoevent = EV | | Event//use when you need to get information about the event6 varDISX = Oevent.clientx-Odiv.offsetleft;7 varDisy = Oevent.clienty-Odiv.offsettop;8 9Document.onmousemove =function(EV) {Ten varoevent = EV | |event; One varL = oevent.clientx-Disx; A vart = oevent.clienty-Disy; - - if(L < 0) { theL = 0; -}Else if(L > Document.documentElement.clientWidth-odiv.offsetwidth) { -L = document.documentElement.clientWidth-Odiv.offsetwidth; - } + - if(T < 0) { +t = 0; A}Else if(T > Document.documentElement.clientHeight-odiv.offsetheight) { att = document.documentElement.clientHeight-Odiv.offsetheight; - } - -ODiv.style.left = l + ' px '; -ODiv.style.top = t + ' px '; - }; in -Document.onmouseup =function () { toDocument.onmousemove =NULL; +Document.onmouseup =NULL; - }; the }; * //Mobile Side $ //draggingPanax Notoginseng //Get node - varblock = document.getElementById ("Right"); the varOW, OH; + //Binding Touchstart Events AOdiv.addeventlistener ("Touchstart",function(e) { the vartouches = E.touches[0]; +OW = Touches.clientx-Odiv.offsetleft; -OH = Touches.clienty-Odiv.offsettop; $ //block sliding default events for pages $Document.addeventlistener ("Touchmove", DefaultEvent,false); -},false); -Odiv.addeventlistener ("Touchmove",function(e) { the vartouches = E.touches[0]; - varOleft = Touches.clientx-OW;Wuyi varOTop = Touches.clienty-OH; the if(Oleft < 0) { -Oleft = 0; Wu}Else if(Oleft > Document.documentElement.clientWidth-odiv.offsetwidth) { -Oleft = (Document.documentElement.clientWidth-odiv.offsetwidth); About } $ODiv.style.left = oleft + "px"; -ODiv.style.top = OTop + "px"; -},false); -Odiv.addeventlistener ("Touchend",function () { ADocument.removeeventlistener ("Touchmove", DefaultEvent,false); +},false); the - functionDefaultEvent (e) { $ E.preventdefault (); the }; the};
PC-Side mobile-end drag and pull implementation