HTML5 drag and drop (example: Drag and Drop Images Between Two div ),

Source: Internet
Author: User

HTML5 drag and drop (example: Drag and Drop Images Between Two div ),

 

Important explanation:

First, set the draggable attribute to true to make the elements (such as images) drag:

The dataTransfer. setData () method sets the data type and value of the dragged data:

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 must call the ondragover eventEvent. PreventDefault () method:

A drop event occurs when data is dragged.

  • PreventDefault () is called to prevent the browser from processing data by default (the default action of the drop event is to open 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.
  • Dragged data is the id of the dragged element ("drag1 ")
  • Append the dragged element to the placed element (target element ).

 

Result ----

Before dragging

After dragging

Drag back

........

Drag and Drop an image between two <div> elements-code list:

 <!DOCTYPE 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.