Detailed description of Drag and drop events (Drag and drop) and html5drag in HTML5
Browser support
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.
HTML5 drag-and-drop instances
<! DOCTYPE html>
Set the element to drag and drop
First, set the draggable attribute to true to make the element drag:
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:
function drag(ev){ev.dataTransfer.setData("Text",ev.target.id);}
In this example, the data type is "Text", and the value is the id ("drag1") of the element that can be dragged ").
Where to put-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()
Place-ondrop
A drop event occurs when data is dragged.
In the above example, the ondrop attribute calls a function, drop (event ):
function drop(ev){ev.preventDefault();var data=ev.dataTransfer.getData("Text");ev.target.appendChild(document.getElementById(data));}
Code explanation:
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 ).
Summary
The above is all the content of this article. I hope this article will be helpful for you to learn or use HTML5. If you have any questions, please leave a message. Thank you for your support.