After the last mobile phone-dragging event, again after the perfect modification, adding elements can not drag out the screen range function, and do a small function interface
PS: Through the rain of the great God guidance.
The code is as follows:
var touchevent = (function () {var odiv = document.getelementsbytagname (' div ') [0],//get draggable element oipt = Docum Ent.getelementsbytagname (' input ') [0],//Record drag element position oIpt1 = document.getelementsbytagname (' input ') [1]; Record contact position Var touchsatrtfunc,touchmovefunc,gettouchevent; var _this = this; var opinion = {//required variable set ODIV:ODIV,OIPT:OIPT,OIPT1:OIPT1,STARTX: "", Starty: "", Startpositionx: "", Startpositiony: "", Elemwidth: "", Elemheight: ", Bywidth:" ", Byheight:" "}//touch point under event Touchsatrtfunc = Fun Ction (e) {e.preventdefault ();//block touch when browser zoom, scroll bar scrolling, etc. var touch = e.touches[0];//Get First Contact var x = number (Touch.pagex ); Page contact x coordinate var y = number (touch.pagey); Page contact Y coordinate//record contact initial position StartX = x; Starty = y; Drag elements distance from the top of the page startpositiony = odiv.offsettop; Drag elements distance from the left side of the page Startpositionx = Odiv.offsetleft; The width of the draggable element elemwidth = odiv.offsetwidth; The height of the draggable element elemheight = odiv.offsetheight; //Web page visible area width bywidth = document.documentElement.clientWidth; Web page visible Area high byheight = document.documentElement.clientHeight;} Move event under touch point Touchmovefunc = function (e) {e.preventdefault ();//block touch when browser zoom, scroll bar scrolling, etc. var touch = e.touches[0];//Get First Contact var x = number (Touch.pagex); Page contact x coordinate var y = number (touch.pagey); Page contact y-coordinate var fnytop = Startpositiony + (y-starty), Fnyleft = Startpositionx + (X-STARTX); Oipt.value = "element position:" +startpositionx + ":" +startpositiony; Oipt1.value = "Contact Location:" +x + ":" +y; Judge move to edge condition if (fnyleft <= 0) {oDiv.style.left = 0; if (fnytop <= 0) {oDiv.style.top = 0; }else if (fnytop > 0 && fnytop < (byheight-elemheight)) {oDiv.style.top = fnytop + ' px '; }else if (fnytop >= (byheight-elemheight)) {oDiv.style.top = byheight-elemheight + ' px '; }}else if (Fnyleft >= (bywidth-elemwidth)) {oDiv.style.left = bywidth-elemwidth + ' px '; if (fnytop <= 0) {oDiv.style.top = 0; }elsE if (fnytop > 0 && fnytop < (byheight-elemheight)) {oDiv.style.top = fnytop + ' px '; }else if (fnytop >= (byheight-elemheight)) {oDiv.style.top = byheight-elemheight + ' px '; }}else if (Fnyleft > 0 && fnyleft < (bywidth-elemwidth)) {oDiv.style.left = fnyleft + ' px '; if (fnytop <= 0) {oDiv.style.top = 0; }else if (fnytop > 0 && fnytop < (byheight-elemheight)) {oDiv.style.top = fnytop + ' px '; }else if (fnytop >= (byheight-elemheight)) {oDiv.style.top = byheight-elemheight + ' px '; }}}var Touchs = function () {Odiv.addeventlistener (' Touchstart ', touchsatrtfunc,false); Odiv.addeventlistener (' Touchmove ', Touchmovefunc,false);} return {Gettouch:touchs,touchsatrtfunc:touchsatrtfunc,touchmovefunc:touchmovefunc}}) ();
When calling this method, simply add touchevent.gettouch (); Can
However, this code also has limitations, currently need to go to the original JS to modify the required drag elements
Mobile phone-side touch Event implementation element Drag effect 2