Implementation of the JavaScript drag-and-drop principle

Source: Internet
Author: User

Basic ideas for the implementation of drag and drop

The basic principle of drag and drop is to move the dragged element according to the movement of the mouse. The movement of the mouse is the change of x and y coordinates; the movement of the elements is the change of style.position top and left. Of course, not at any time to move the mouse to cause the movement of elements, but should determine whether the state of the left mouse button is pressed, whether it is pressed on the elements can be dragged.

Based on the above principles, I have written the following basic ideas. The sense code is still relatively short,

  1. View Plaincopy to Clipboardprint
  2. Drag state = 0 when the mouse is pressed on the element {
  3. Drag state = 1
  4. Record the x and y coordinates of the mouse
  5. Record the x and y coordinates of the element
  6. }
  7. When the mouse moves on the element {
  8. If the drag state is 0, do nothing.
  9. If the drag state is 1, then
  10. Element y = Now mouse y-Original mouse y + original element y
  11. Element x = Now mouse x-original mouse x + original element x
  12. }
  13. When the mouse is released at any time {
  14. Drag state = 0
  15. }

The above ideas can be translated into JS code to achieve the effect of drag and drop.

JavaScript code

  1. View Plaincopy to Clipboardprint
  2. <mce:script type="Text/javascript" ><!--
  3. var dragging = false;
  4. var test;
  5. var Mousey;
  6. var mousex;
  7. Window.onload = function () {
  8. Test = document.getElementById ("test");
  9. Test.onmousedown = down;
  10. Test.onmousemove = move;
  11. Document.onmouseup = up;
  12. Test.style.position = "relative";
  13. Test.style.top = "0px";
  14. Test.style.left = "0px";
  15. }
  16. function Down (event)
  17. {
  18. Event = Event | | window.event;
  19. dragging = true;
  20. MouseX = parseint (EVENT.CLIENTX);
  21. Mousey = parseint (Event.clienty);
  22. Objy = parseint (test.style.top);
  23. OBJX = parseint (test.style.left);
  24. }
  25. function Move (event) {
  26. Event = Event | | window.event;
  27. if (dragging = = true) {
  28. var x, y;
  29. y = Event.clienty-mousey + objy;
  30. x = Event.clientx-mousex + objx;
  31. Test.style.top = y + "px";
  32. Test.style.left = x + "px";
  33. }
  34. }
  35. function up () {
  36. dragging = false;
  37. }
  38. //--></mce:script>

HTML code

  1. View Plaincopy to Clipboardprint
  2. <div id="test" style="border:1px solid; width:400px; Background: #CCCCCC; " >
  3. <P> I'm dragging a sample div. </P>
  4. <p> You can test the effect. </P>
  5. </div>

Demo Code

    1. + Expand Sourceview plaincopy to Clipboardprint

shortcomings and the place to be perfected

This drag-and-drop example is quite imperfect, but it has already achieved the most fundamental core of drag and drop. Think of the perfect part of the following points: The method of obtaining the X and y coordinates of an element is simplified in this article; This article only lets a fixed element has the drag function, the flexibility is too poor, the mouse style does not have the transformation, the quick "disorderly drag and drop" has the certain probability to have the question. One of the last problems temporarily do not know how to solve, the front three more good to do.

Implementation of the JavaScript drag-and-drop principle

Related Article

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.