Implement Div drag and drop using JavaScript

Source: Internet
Author: User

Hands-on exercises can really discover many problems. If there is a problem, it is better to solve it early. Haha ~ Still confused ~ I don't know where to find the answer. Remember it first, and I will look back later ~

<! Doctype HTML> 

After reading the tutorial, follow your own understanding and then write this drag to find a bug. This is an event that adds mouse clicks and loose to the DIV object, and the display is normal. However, when we click this Div and drag it beyond the visible area of the browser and then release the mouse, we can see that it does not trigger the onmouseup event of the Div. Therefore, the DIV keeps moving with the mouse without clicking it.

Then go back to the tutorial code, as shown below:

<! Doctype HTML> 

In this tutorial, The onmouseup event is bound to the document, so that the mouse moves out of the visible area and the event can still be triggered when it is released, so everything is normal. Then I began to get confused again. Why should onmouseup be written in onmousedown? It is reasonable to say that although the two of them have a forward and backward relationship, they click "release" first, but there is no such relationship, just like moving the mouse in and out, I usually write it out in the function, so I gave it onmouseup. The result is wrong, and Div keeps moving with the mouse, releasing it does not seem to trigger the onmouseup function ....

<! Doctype HTML> 

After reading the code several times, you can see that this function is actually triggered. The onmouseup is bound to the document, and the onmouseup is cleared when the mouse is released, that is, this function is executed only once. So we can remove the document. onmouseup = NULL; above.

Confused 1. When the onmouseup event is bound to the document, the event on the document can still be triggered when the mouse is removed from the div;

Confused 1. Here, the two sentences in the box empty the onmousemove and onmouseup functions. Why can these two functions be triggered when you click Div for the second time? Isn't the content in them cleared?

Confused 2. Despite multiple clicks, the function in the red box below is only executed once, and only one window is displayed. How many executions can be performed above ???

I am also busy solving problems when I pass ~

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.