Native JS drag and drop (first lesson not compatible) drag ideas _javascript skills

Source: Internet
Author: User
Copy code code as follows:
/*
Drag-and-drop process:
1, first click on the element you want to drag
2, the element under Click is selected, Move moves
3, when the mouse is bouncing Wait, stop the action
4, click Element Odiv, the available is the Odiv area, and move and up is the global area, that is, the entire document universal, that is, should use document
/
Odiv = document.getElementById ("Gaga");
Odiv.onmousedown = function (e) {//When the mouse clicks down
var diffx = e.clientx-odiv.offsetleft;//Click on the location from the leftmost position of the browser (CLI ENTX) subtracting the clicked element from the leftmost position (Odiv.offetleft) is indeterminate at the left-most position of the clicked element, so this result is required
var diffy = e.clienty-odiv.offsettop;//Point The position of the hit from the top of the browser (ClientY) minus the position of the clicked element from the top (odiv.offsettop) is indeterminate at the top position of the clicked element, so this result is required
Document.onmousemove = Function (e) {//When the mouse button is pressed
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 is bouncing
Document.onmousemove = null;//empty mouse down event
Document.onmous EuP = null; Empty the mouse to bounce the event
}
};

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.