Native js drag (incompatible with the first lesson)

Source: Internet
Author: User

Copy codeThe Code is as follows:
/*
Drag process:
1. Click the element to be dragged.
2. The element under the click is selected and moved.
3. When the mouse is up, stop the action.
4. When you click the oDIV element, the oDIV area is available, and move and up are global regions, that is, the entire document is used.
*/
ODIV = document. getElementById ("gaga ");
ODIV. onmousedown = function (e) {// when the mouse clicks down
Var diffX = e. clientX-oDIV. offsetLeft; // The position to be clicked is the leftmost position (clientX) of the browser, and then the position to the leftmost from the clicked element (oDIV. offetLeft) the leftmost position of the element clicked in the index is uncertain. Therefore, this result is required.
Var diffY = e. clientY-oDIV. offsetTop; // The clicked position is located at the top of the browser (clientY), and then the position between the clicked element and the top (oDIV. offsetTop) the distance between the clicked element and the top of the index is unknown. Therefore, this result is required.
Document. onmousemove = function (e) {// when you press the mouse
Var e = e | window. event;
ODIV. style. top = e. clientY-diffY + "px ";
ODIV. style. left = e. clientX-diffX + "px ";
};
Document. onmouseup = function () {// when the mouse pops up
Document. onmousemove = null; // clear the events that do not move the mouse
Document. onmouseup = null; // clear the mouse
}
};

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.