Mobile phone-side touch Event implementation element Drag effect 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.