dataTransfer Object and Drag&Drop
dataTransfer object belongs to the event object during the drag&drop operations. It provides a way of storing temporary data for use during drag&drop. Let me show you what you can do with the dataTransfer object.
- Storing temporary data:
Data about the dragged DOM element can be stored in dataTransfer using setData() method and be retrieved during the drop stage.
- Setting draged element icon:
When you drag an element on the web page, you can set an custom icon near the mouse cursor to indicate that the element is being dragged.
- Creating visual drag&drop effect:
There are three drag&drop effects available, copy, move and link. They are mostly used to produce visual feedback to users when the dragged element is over the dropped area. The default drop effect for chrome is “copy”.
Besides the above three key points for dataTransfer object, let’s recap other important points to note for performing a drag&drop operation.
- Make the element draggable:
Not all elements in HTML are draggable by default. For example, images are draggable by default. For most of the elements, you have to set the draggable attribute to true in order to perform drag&drop operations.
- Drag&drop events:
There are eight drag&drop events of which three are for drag events and five are for drop events:
- Preventing default behavior:
When performing drag&drop actions, the default behavior for some of the events must be prohibited, namely the default behavior for dragover event. Of course you can prevent the default behavior for event of every drag&drop stage to meet the browser compatibility requirement.
You can find more information on the mozilla developer network websitemozillar developer network