HTML | Drag and Drop
Drag and Drop is a very interactive and user-friendly concept which makes it easier to move an object to a different location by grabbing it. This allows the user to click and hold the mouse button over an element, drag it to another location, and release the mouse button to drop the element there. In HTML 5 Drag and Drop are much easier to code and any element in it is draggable.
Drag and Drop Events: There are many of Drag and Drop events some of them are listed below:
Events Description ondragstart Calls a function, drag(event), that specifies what data to be dragged ondragenter To determine whether or not the drop target is to accept the drop. If the drop is to be accepted, then this event has to be canceled ondragleave Occurs when the mouse leaves an element before a valid drop target while the drag is occurring ondragover Specifies where the dragged data can be dropped ondrop Specifies where the drop was occurred at the end of the drag operation ondragend Occurs when the user has finished dragging an element
The Data Transfer Object: The data transfer property is used when the whole process of Drag and Drop happens. It is used to hold the dragged data from the source and drop it to the desired location. These are the properties associated with it:
Attributes Description dataTransfer.setData(format, data) Sets the data to be dragged dataTransfer.clearData(format) Calling this function with no argument clears all the data. Calling it with a format argument removes only that specific data. dataTransfer.getData(format) Returns the data of the specified format. If there is no such data, returns the empty string. dataTransfer.types This property returns an array of all the formats that were set in the dragstart event. dataTransfer.files It is used to return all the files that are to be dropped. dataTransfer.setDragImage(element, x, y) It is used to display an existing image as the drag image instead of the default image alongside the cursor. The coordinates specify the drop location dataTransfer.addElement(element) Adds the specified element to be drawn as a drag feedback image dataTransfer.effectAllowed(value) Tells the browser that only the listed type(s) of operations are allowed for the user. The property can be set to the following values: none, copy, copyLink, copyMove, link, linkMove, move, all, and uninitialized. dataTransfer.dropEffect(value) Controls the feedback that the user is given during the dragenter and dragover events. When the user hovers over a target element, the browser’s cursor will indicate what type of operation is going to take place (e.g. a copy, a move, etc.). The effect can take on one of the following values: none, copy, link, move.
Procedure for Drag and Drop:
- Step 1: Make an object draggable
- First set the draggable attribute to true for that element to be draggable <img draggable = “true”>
- Then, specify what should happen when the element is dragged. The ondragstart attribute calls a function, drag(event), that specifies what data to be dragged. The dataTransfer.setData() method sets the data type and the value of the dragged data
- The event listener ondragstart will set the allowed effects (copy, move, link, or some combination).
- Step 2: Dropping the Object
- The ondragover event specifies where the dragged data can be dropped. By default, data/elements cannot be dropped in other elements. To allow a drop, it must prevent the default handling of the element. This is done by calling the event.preventDefault() method
- Finally, the drop event, which allows the actual drop to be performed
- Start the drag by setting the draggable property of the element to be dragged to true.
- Get the dragged data with the dataTransfer.getData() method. This method will return any data that was set to the same type in the setData() method.
- Call event.preventDefault() method to allow dropping of elements in other elements by preventing the default handling of the element.
- The element is stored in the variable data which we append into the drop element.
Example 2: This example illustrate the image drag and drop.
Attention reader! Don’t stop learning now. Get hold of all the important Comcompetitivepetitve Programming concepts with the Web Design for Beginners | HTML course.