JS Simple Drag effect

Source: Internet
Author: User

Principle: Register the MouseMove event, make the element follow the mouse to move, register the MouseUp event, remove the MouseMove event, so that the element no longer follows the move when releasing the mouse, can be fixed in the specified Position. Registers the MouseMove event and the MouseUp event in the MouseDown event so that a complete drag can be Completed.

Focus: the application of SetCapture () in Ie. Its role is to capture all the mouseevent, after the settings have been completed, even mouse-out Windows registered mouse events can still be triggered. What it does here is that when the mouse moves too fast out of the bounds of the element, the MouseMove event that would otherwise fail will still Work. In the FF and chrome, because there is no corresponding settings, you can assign MouseMove and MouseUp events to the docuemnt object, so no matter how the mouse moves, all on the document, in fact, in ie, so write, then do not set the SetCapture (), the only difference between it and the set is that when the element moves right out of the window boundary, the element is always within the visible range.

JS Code
    1. <!doctype html>
    2. <style type="text/css" >
    3. #drag {border:1px Solid blue;width:100px;height:100px;position:absolute;}
    4. </style>
    5. <body>
    6. <input id="x"/>
    7. <input id="y"/>
    8. <div id="drag" ></div>
    9. <script><!--
    10. var drag = document.getElementById (' drag ');
    11. var inputx = document.getElementById (' x ');
    12. var inputy = document.getElementById (' y ');
    13. If (document.attachevent) {
    14. Drag.attachevent (' onmousedown ', draghandle);
    15. }else{
    16. Drag.addeventlistener (' MouseDown ', draghandle,false);
    17. }
    18. function Draghandle (event) {
    19. var event = event| |  window.event;
    20. var StartX = drag.offsetleft;
    21. var starty = drag.offsettop;
    22. var mousex = event.clientx;
    23. var mousey = event.clienty;
    24. var deltax = mousex-startx;
    25. var deltay = mousey-starty;
    26. if (document.attachevent) {
    27. Drag.attachevent (' onmousemove ', movehandle);
    28. Drag.attachevent (' onmouseup ', uphandle);
    29. Drag.attachevent (' onlosecapture ', uphandle);
    30. Drag.setcapture ();
    31. }else{
    32. Document.addeventlistener (' MouseMove ', movehandle,true);
    33. Document.addeventlistener (' MouseUp ', uphandle,true);
    34. }
    35. function Movehandle (event) {
    36. var event = event| |  window.event;
    37. Drag.style.left = (event.clientx-deltax) +"px";
    38. Drag.style.top = (event.clienty-deltay) +"px";
    39. inputx.value=drag.style.left;
    40. inputy.value=drag.style.top;
    41. }
    42. function Uphandle () {
    43. if (document.attachevent) {
    44. Drag.detachevent (' onmousemove ', movehandle);
    45. Drag.detachevent (' onmouseup ', uphandle);
    46. Drag.detachevent (' onlosecapture ', uphandle);
    47. Drag.releasecapture ();
    48. }else{
    49. Document.removeeventlistener (' MouseMove ', movehandle,true);
    50. Document.removeeventlistener (' MouseUp ', uphandle,true);
    51. }
    52. }
    53. }
    54. --</script>
    55. </body>

JS Simple Drag effect

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.