HTML5 drag-and-drop implementation code, html5 drag-and-drop implementation code

Source: Internet
Author: User

HTML5 drag-and-drop implementation code, html5 drag-and-drop implementation code
In HTML5, drag and drop is part of the standard, and any element can be dragged and dropped. The specific content is as follows:

1. Drag and Drop 

Copy XML/HTML Code to clipboard
  1. <! Doctype html>
  2. <Html>
  3. <Head>
  4. <Style type = "text/css">
  5. # Div1 {width: 350px; height: 70px; padding: 10px; border: 1px solid # aaaaaa ;}
  6. </Style>
  7. <Script>
  8. Function allowDrop (ev)
  9. {
  10. Ev. preventDefault ();
  11. }
  12. Function drag (ev)
  13. {
  14. Ev. dataTransfer. setData ("Text", ev.tar get. id );
  15. }
  16. Function drop (ev)
  17. {
  18. Ev. preventDefault ();
  19. Var data = ev. dataTransfer. getData ("Text ");
  20. Ev.tar get. appendChild (document. getElementById (data ));
  21. }
  22. </Script>
  23. </Head>
  24. <Body>
  25. <P> drag the W3CSchool. cc image to the rectangle: </p>
  26. <Div id = "div1" ondrop = "drop (event)" ondragover = "allowDrop (event)"> </div>
  27. <Br>
  28. </Body>
  29. </Html>

2. Set the element to drag and drop
 
First, set the draggable attribute to true:
 
3. What to drag-ondragstart and setData ()
 
Then, specify what will happen when the element is dragged. In the preceding example, the ondragstart attribute calls a function, drag (event), which specifies the data to be dragged. The dataTransfer. setData () method sets the data type and value of the dragged data:

Copy the content to the clipboard using JavaScript Code
  1. Function drag (ev)
  2. {
  3. Ev. dataTransfer. setData ("Text", ev.tar get. id );
  4. }

In this example, the data type is "Text", and the value is the id ("drag1") of the element that can be dragged ").
 
4. Where to put it-ondragover
 
The ondragover event specifies where to place the dragged data. By default, data/elements cannot be placed in other elements. To allow placement, We must block the default Processing Method for elements. You need to call the event. preventDefault () method of the ondragover event:Event. preventDefault ()
 
5. Place-ondrop
 
A drop event occurs when data is dragged. In the above example, the ondrop attribute calls a function, drop (event ):

Copy the content to the clipboard using JavaScript Code
  1. Function drop (ev)
  2. {
  3. Ev. preventDefault ();
  4. Var data = ev. dataTransfer. getData ("Text ");
  5. Ev.tar get. appendChild (document. getElementById (data ));
  6. }

Code explanation:

PreventDefault () is called to prevent the browser from processing data by default (the default action of the drop event is to open it in the form of a link ). Use the dataTransfer. getData ("Text") method to obtain the dragged data. This method returns any data set to the same type in the setData () method. The dragged data is the id of the dragged element ("drag1 "). Append the dragged element to the placement element (target element.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.