How does HTML5 implement drag-and-drop of elements?

Source: Internet
Author: User

How does HTML5 implement drag-and-drop of elements?

I am afraid many front-end users do not know how to implement HTML5 drag-and-drop operations. This article describes how to implement HTML5 drag-and-drop operations. Sort the backups for future reference.

Example 1:

Index.html

Copy XML/HTML Code to clipboard
  1. <! Doctype html>
  2. <Html>
  3. <Head>
  4. <Meta charset = "UTF-8">
  5. <Title> Drag </title>
  6. <Style>
  7. . Box {
  8. Width: 400px;
  9. Height: 400px;
  10. Float: left;
  11. }
  12. # Box1 {
  13. Background: # CCC;
  14. }
  15. # Box2 {
  16. Background: # FF0;
  17. }
  18. </Style>
  19. </Head>
  20. <Body>
  21. <Div id = "box1" class = "box"> </div>
  22. <Div id = "box2" class = "box"> </div>
Copy the content to the clipboard using JavaScript Code
  1. <Script src = "app1.js"> </script>
  2. </Body>
  3. </Html>
  4. App1.js
  5. /**
  6. * App1.js
  7. */
  8. Var oBox1,
  9. OBox2,
  10. OImg1;
  11. Window. onload = function (){
  12. OBox1 = document. getElementById ('box1 ');
  13. OBox2 = document. getElementById ('box2 ');
  14. OImg1 = document. getElementById ('img1 ');
  15. //
  16. OBox1.ondragover = oBox2.ondragover = function (e ){
  17. E. preventDefault ();
  18. };
  19. //
  20. OImg1.ondragstart = function (e ){
  21. E. dataTransfer. setData ('text', e.tar get. id );
  22. };
  23. OBox1.ondrop = dropImg;
  24. OBox2.ondrop = dropImg;
  25. };
  26. Function dropImg (e ){
  27. E. preventDefault ();
  28. Var tempImg = document. getElementById (e. dataTransfer. getData ('text '));
  29. E.tar get. appendChild (tempImg );
  30. }

Knowledge points involved

The following events are triggered during drag-and-drop:
Trigger an event (Source Element) on the drag target)
Ondragstart-triggered when the user starts to drag an element
Ondrag-triggered when the element is being dragged
Ondragend-triggered after the user completes element dragging

Events triggered when the target is released
Ondragenter-this event is triggered when an object dragged by the mouse enters its container range.
Ondragover-this event is triggered when a dragged object is dragged within the range of another object container
Ondragleave-this event is triggered when an object dragged by the mouse leaves its container range.
Ondrop-this event is triggered when the mouse key is released during a drag process.

Event object (replaced by "e)

E.tar get

The description on W3Cschool is: the elements that trigger this event (the target node of the event) are returned. This target attribute is only compatible with ie9 and later versions.

E. preventDefault ()

Cancels the default action of an event.

E. dataTransfer. setData ()

Set the data type and value of the dragged data:
The Code is as follows: e. dataTransfer. setData ("Text", ev.tar get. id); // The first parameter is Text (lowercase)

E. dataTransfer. getData ()

Get dragged data:
The Code is as follows: e. dataTransfer. getData ("Text ");

The above is all the content of this article, hoping to help you learn.

Original article: http://www.cnblogs.com/oovwall/p/5213580.html

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.