
- Published on
- /4 mins read/---
I created an example about Javascript Drag-n-Drop APIs at https://hta218.github.io/dnd-keynotes
Below are some interesting key-notes I've learned about the APIs.
Basic concepts
A typical drag operation begins when a user selects a draggable element, drags the element to a dropzone, and then releases the dragged element.
Events:
| Event | Fires when… |
|---|---|
| drag | …a dragged item (element or text selection) is dragged. |
| dragend | …a drag operation ends (such as releasing a mouse button or hitting the Esc key) |
| dragenter | …a dragged item enters a valid drop target. |
| dragexit | …an element is no longer the drag operation's immediate selection target. |
| dragleave | …a dragged item leaves a valid drop target. |
| dragover | …a dragged item is being dragged over a valid drop target, every few hundred milliseconds. |
| dragstart | …the user starts dragging an item. |
| drop | …an item is dropped on a valid drop target. |
Keynotes
To make an element draggable, add
draggable="true"attributedragstart
dragstartis the first event fired when a drag operation starts on a draggable elementuse
e.dataTransfer.setData()method to set any drag's data, this will stay during the drag operation
- If you don't want the translucent image generated from the drag target during drag, use
e.dataTransfer.setDragImage()to change it
The dropEffect
- The dropEffect property is used to control the feedback the user is given during a drag-and-drop operation
dropEffectproperty could be:move: dragged data will be moved to the dropzone.copy: dragged data will be copied to the dropzone.- ..
The dropzone
To make an element becomes a dropzone, it must have both dragover and drop event handler.
Remember to call e.preventDefault() in
dragoverhandler or the browser won't let you drop anything inside
- Keep mind that we can only use the
dataTransfer.getData()in thedrop-handler(it will return empty string inside dragover or dragenter handler)
dragend
- The dragend event fires after a drag operation finished regardless of whether the drag completed or was canceled
- If the drag operation failed, the value of
e.dataTransfer.dropEffectwill be"none"
