Posts in current category
May 03, 2021 13:05 HTML5
The purpose of drag-and-drop is to allow you to place an object where you want to place it.
Drag and drop are part of the HTML5 standard.
Drag the w3cschool icon into the rectangular box.
Drag-and-drop is a common feature that grabs an object and drags it to another location.
In HTML5, drag-and-drop is part of the standard, and any element can drag-and-drop.
Internet Explorer 9 Plus, Firefox, Opera, Chrome, and Safari support drag.
Note: Safari 5.1.2 does not support drag.
The following example is a simple drag-and-drop example:
It may seem complicated, but we can look at the different parts of the drag-and-drop event separately.
First, to make the element draggable, set the dragable property to true:
Then, specify what happens when an element is dragged.
In the example above, the ondragstart property calls a function, drag, which specifies the data being dragged.
The dataTransfer.setData() method sets the data type and value of the dragged data:
In this example, the data type is "Text" and the value is the id ("drag1") of the draggable element.
Tip: You can learn more about the "ondragstart event" section of this site!
The ondragover event specifies where to place the dragged data.
By default, data/elements cannot be placed in other elements. If we need to set allow placement, we must block the default processing of elements.
This is by calling the event.preventDefault() method of the ondragover event:
Drop events occur when dragged data is placed.
In the example above, the ondrop property calls a function, drop:
Drag and drop pictures back and forth
How to drag and drop an image between two elements.
HTML DOM Reference Manual: Ondrop Events