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,
- View Plaincopy to Clipboardprint
- Drag state = 0 when the mouse is pressed on the element {
- Drag state = 1
- Record the x and y coordinates of the mouse
- Record the x and y coordinates of the element
- }
- When the mouse moves on the element {
- If the drag state is 0, do nothing.
- If the drag state is 1, then
- Element y = Now mouse y-Original mouse y + original element y
- Element x = Now mouse x-original mouse x + original element x
- }
- When the mouse is released at any time {
- Drag state = 0
- }
The above ideas can be translated into JS code to achieve the effect of drag and drop.
JavaScript code
- View Plaincopy to Clipboardprint
- <mce:script type="Text/javascript" ><!--
- var dragging = false;
- var test;
- var Mousey;
- var mousex;
- Window.onload = function () {
- Test = document.getElementById ("test");
- Test.onmousedown = down;
- Test.onmousemove = move;
- Document.onmouseup = up;
- Test.style.position = "relative";
- Test.style.top = "0px";
- Test.style.left = "0px";
- }
- function Down (event)
- {
- Event = Event | | window.event;
- dragging = true;
- MouseX = parseint (EVENT.CLIENTX);
- Mousey = parseint (Event.clienty);
- Objy = parseint (test.style.top);
- OBJX = parseint (test.style.left);
- }
- function Move (event) {
- Event = Event | | window.event;
- if (dragging = = true) {
- var x, y;
- y = Event.clienty-mousey + objy;
- x = Event.clientx-mousex + objx;
- Test.style.top = y + "px";
- Test.style.left = x + "px";
- }
- }
- function up () {
- dragging = false;
- }
- //--></mce:script>
HTML code
- View Plaincopy to Clipboardprint
- <div id="test" style="border:1px solid; width:400px; Background: #CCCCCC; " >
- <P> I'm dragging a sample div. </P>
- <p> You can test the effect. </P>
- </div>
Demo Code
- + 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