Js Implementation of the drag closure function detailed introduction _ javascript skills

Source: Internet
Author: User
During the development process, JavaScript may be used to implement drag-and-drop functions. This article will introduce this issue in depth. For more information, see Js drag

Simple closure implementation

The Code is as follows:


/**
* Created with JetBrains WebStorm.
* User: lsj
* Date: 12-11-24
* Time: PM
* To change this template use File | Settings | File Templates.
*/
Var dragmanager = (function ()
{
// Identify the Z axis coordinate of the moving Element
Var index_z = 1;
// The current drag Element
Var drganow;
// Move the identifier
Var dragbegin = false;
// Left distance from p when you click the mouse
Var relativex = 0;
// Distance from the top p when the mouse clicks
Var relativey = 0;
// Identifies whether the mouse is removed
Var isout = false;
Return {
/**
* Bind the mouse to the document to raise an event, which mainly prevents the mouse from moving too quickly out of the el area.
*/
BingDocOnMouseUp: function ()
{
// Register the global onmouseup event to prevent the mouse and el from being out of sync due to excessive mouse movement.
Document. onmouseup = function (e)
{
Var ev = window. event | e;
If (isout & dragbegin)
{
// Change the relative position of p
Drganow. style. left = (ev. clientX-relativex) + 'px ';
Drganow. style. top = (ev. clientY-relativey) + 'px ';
Drganow. style. cursor = 'normal ';
Dragbegin = false;
Isout = false;
}
}
},
/**
* Bind injected elements to events.
* @ Param el
*/
RegisterElementEv: function (element)
{
Element. onmousedown = function (e)
{
Var ev = window. event | e;
Var clientx = ev. clientX;
Var clienty = ev. clientY;
Var left = parseInt (this. style. left. substring (0, this. style. left. indexOf ("p ")));
Var top = parseInt (this. style. top. substring (0, this. style. top. indexOf ("p ")));
Relativex = clientx-left;
Relativey = clienty-top;
Index_z ++;
This. style. zIndex = index_z;
Drganow = this;
Dragbegin = true;
}
Element. onmousemove = function (e)
{
Var ev = window. event | e;
// Start dragging
If (dragbegin)
{
// Change the relative position of p
This. style. left = (ev. clientX-relativex) + 'px ';
This. style. top = (ev. clientY-relativey) + 'px ';
This. style. cursor = 'move ';
}
}
Element. onmouseout = function (e)
{
Isout = true;
}
Element. onmouseup = function (e)
{
Var ev = window. event | e;
If (dragbegin)
{
// Change the relative position of p
Drganow. style. left = (ev. clientX-relativex) + 'px ';
Drganow. style. top = (ev. clientY-relativey) + 'px ';
Drganow. style. cursor = 'normal ';
Dragbegin = false;
}
}
}
}
})();


1. Implemented in the form of closures to facilitate later maintenance. All the variables required for the mobile process are transferred to gridmanager.
2. During the drag process, the mouse moves too fast, causing the moving element to keep up with the mouse. Therefore, to register the document. oumouseup event, the switch of this event is triggered by the onmouseout event of the moving element.
3. During the drag operation, the select event of the browser's onmousemove may be triggered. in ie, onmousemove = "document. selection. empty ()" can be blocked ()"
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.