PC-Side mobile-end drag and pull implementation

Source: Internet
Author: User

#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

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.