HTML5 Mobile Development Road (16)--Magical drag and drop function

Source: Internet
Author: User

This paper is the official HTML5 training course for Brother Lian it education organization, mainly introduces: HTML5 Mobile Development Road (16)--Magical drag and drop function

In the rapid development of smart phones now drag-and-drop functionality has become a fashion, but in our browser is not yet missing this convenient function? There are standards for drag and drop in HTML5 's new standard, and any element can be dragged and dropped as part of the HTML5 standard.

First, browser support situation

Internet Explorer 9, Firefox, Opera 12, Chrome, and Safari 5 support drag and drop.

Note: Drag-and-drop is not supported in Safari 5.1.2.

Ii. Methods of Use

First, to make an element draggable, set the Draggable property to True:

[HTML] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. <img draggable="true" />

Then, what happens when the specified element is dragged

Let the Ondragstart property call a function and return a drag object to the function

[HTML] view plain copy

 print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" View the Code slice "width=" and "height=" style = "border:0px;"/>650) this.width=650; "Src=" Https://code.csdn.net/assets/ico_ Fork.svg "alt=" To My Code slice "width=" "height=" "style=" border:0px "/>

    1. <img id="Drag1" src="img_logo.gif" draggable="true"

    2. ondragstart="Drag (Event)" width="336" height=" "/>


The Datatransfer.setdata () method sets the data type and value of the dragged data: (we pass the ID of the dragged object to DataTransfer)

[HTML] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. function drag (EV)

    2. {

    3. <span style="White-space:pre"> </span>ev.datatransfer.setdata ("Text",  Ev.target.id);

    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. If you need to set allow placement, we must block the default handling of elements.

This is done by calling the Event.preventdefault () method of the OnDragOver event:

[HTML] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

    1. Event.preventdefault ()

The drop event occurs when the dragged data is dropped.

Above, the OnDrop property invokes a function, Drop (event):


[HTML] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

  1. function Drop (EV)

  2. {

  3. <span style="White-space:pre"> </span>ev.preventdefault ();

  4. <span style="White-space:pre"> </span>var data=   Ev.dataTransfer.getData ("Text");

  5. <span style="White-space:pre"> </span>ev.target.appendchild (  document.getElementById (data));

  6. }

Code Explanation:
    • Call Preventdefault () to avoid browser default handling of data (the default behavior of the drop event is to open as a link)

    • The dragged data is obtained by means of the Datatransfer.getdata ("Text") method. The method returns any data that is set to the same type in the SetData () method.

    • The dragged data is the ID of the dragged element ("Drag1")

    • Appends the dragged element to the placement element (the target element)

Ii. Description of the case

[HTML] view plain copy

Print? 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "alt=" on Code View "Width=" "height=" 12 " Style= "border:0px;"/>650) this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "alt=" Derivation to My Code slice " Width= "height=" style= "border:0px;"/>

  1. <! DOCTYPE HTML>

  2. <html>

  3. <head>

  4. <script type="Text/javascript">

  5. Block the default handling of elements

  6. function AllowDrop (EV)

  7. {

  8. Ev.preventdefault ();

  9. }

  10. Pass the ID of the dragged object to the DataTransfer intermediate object

  11. function drag (EV)

  12. {

  13. Ev.dataTransfer.setData ("Text", ev.target.id);

  14. }

  15. Get Drag Object ID Find object instance and add Dom model to <div> inside

  16. function Drop (EV)

  17. {

  18. Ev.preventdefault ();

  19. var data=ev.dataTransfer.getData ("Text");

  20. Ev.target.appendChild (document.getElementById (data));

  21. }

  22. </Script>

  23. </head>

  24. <body>

  25. <!--the place to be dragged--

  26. <div id="Div1" ondrop="Drop (event)"

  27. ondragover="AllowDrop (event)"

  28. style="border:1px solid red;width:300px;height:200px;" >

  29. </div>

  30. <!--objects to be dragged--

  31. <img id="Drag1" src="test.png" draggable="true"

  32. ondragstart="Drag (event)"/>

  33. </Body>

  34. </html>

650) this.width=650; "src=" Http://img.my.csdn.net/uploads/201401/12/1389505406_5925.gif "style=" border:0px; "/>


HTML5 Mobile Development Road (16)--Magical drag and drop function

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.