JS drag element principle and implementation code

Source: Internet
Author: User

Drag-and-drop function is mainly used to let the user do some custom actions, such as drag sort, pop-up box drag move, etc., the effect is pretty good. The following explains the principle of drag and drop, I hope to help the needy friends!

I. Drag-and-drop process action
① mouse button down
② Mouse Movement
③ Mouse Release

Second, the corresponding JS event in the drag-and-drop process
① mouse button will trigger onmousedown event

[JavaScript]View PlainCopy
    1. Obj.onmousedown = function (e) {
    2. //..........  
    3. }

② mouse movement triggers onmousemove events

[JavaScript]View PlainCopy
    1. Obj.onmousemove = function (e) {
    2. //......  
    3. }

③ Mouse Loose meeting trigger onmouseup Event

[JavaScript]View PlainCopy
    1. Obj.onmouseup = function () {
    2. //......  
    3. }


Three, the realization of the principle of explanation
Drag is actually achieved by getting the distance of the mouse movement, which is the coordinate (x, y) difference between the coordinates (x, y) of the position before the move and the position in the movement.
When you press or move the mouse, you can get to the current position of the mouse, that is, the position before the move and the position in the move.
So the code above ① and ② should be like this.

[JavaScript]View PlainCopy
    1. var mousedownx,mousedowny //due to the need to calculate the mouse offset in the move needs to use the mouse down the coordinates, solid declaration called global variables
    2. Obj.onmousedown = function (e) {
    3. Mousedownx = E.pagex;
    4. Mousedowny = E.pagey;
    5. }
    6. Obj.onmousemove = function (e) {
    7. var Mousemovex = E.pagex,mousemovey = E.pagey;
    8. }

Before and after moving the coordinates have, then calculate the offset, look first

It is obvious that the x-coordinate of the moved element is the x-coordinate of the mouse movement-the x-coordinate of the mouse press + the initial x-coordinate of the element
The y-coordinate is the y-coordinate of the mouse movement-the y-coordinate of the mouse press + the initial y-coordinate of the element
The x, y of the new x, y replacement element is taken care of.
Then the code should be replaced with:

[JavaScript]View PlainCopy
  1. var mousedownx,mousedowny,initx,inity,flag = false;
  2. Obj.onmousedown = function (e) {
  3. //x, y coordinates of mouse when mouse is pressed
  4. Mousedownx = E.pagex;
  5. Mousedowny = E.pagey;
  6. //x, y coordinates of the initial position
  7. INITX = Obj.offsetleft;
  8. inity = Obj.offsettop;
  9. //Indicates that the mouse has been pressed
  10. Flag = true;
  11. }
  12. Obj.onmousemove = function (e) {
  13. //Make sure the mouse is pressed
  14. if (flag) {
  15. var Mousemovex = E.pagex,mousemovey = E.pagey;
  16. this.style.left = parseint (Mousemovex)-parseint (MOUSEDOWNX) + parseint (INITX) + "px";
  17. this.style.top = parseint (Mousemovey)-parseint (Mousedowny) + parseint (inity) + "px";
  18. }
  19. }
  20. Obj.onmouseup = function () {
  21. //logo has released the mouse
  22. Flag = false;
  23. }

Note that if you write with the jquery library, the three events are MouseDown, MouseMove, MouseUp, with a slightly different name.

Also, the style of the dragged element is set to an absolute or relative position to be effective.

At this point, the simplest element drag-and-drop function is finished ~ ~ ~

If there is an incorrect place, please correct me!

JS drag element principle and implementation code

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.