Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a defined ordering between dragend and drop events?

According to the documentation on the HTML5 drag and drop API, two events are fired when an element is dropped:

  1. A drop event is fired from the drop target
  2. A dragend event is fired from the source of the drag

In doing a simple test (see snippet), the drop event always fires just before the dragend event (at least in Chrome) but I can't find anything about the ordering of these events in the spec.

Is the ordering of these events defined, or are they free to fire in either order?

function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    console.log("drop at " + Date.now());
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
}

function dragend(ev) {
    console.log("dragend at " + Date.now());
}
#div1 {
  background-color: red;
  height: 100px;
  width: 100px;
}
#drag1 {
  background-color: green;
  height: 50px;
  width: 50px;
}
<div>Drag the green square in to the red one</div>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" width="100px" height="100px"></div>

<div id="drag1" draggable="true" ondragstart="drag(event)" ondragend="dragend(event)" width="50px" height="50px">
like image 743
robertjlooby Avatar asked Jun 29 '16 23:06

robertjlooby


People also ask

How does drag and drop work?

The basic sequence involved in drag and drop is: Move the pointer to the object. Press, and hold down, the button on the mouse or other pointing device, to "grab" the object. "Drag" the object to the desired location by moving the pointer to this one.

How do you implement drag and drop?

Most modern web browsers have implemented native drag-and-drop based on the HTML5 spec. By default, only image and text can be draggable. To drag an image, you simply hold the mouse button down and then move it. To drag the text, you need to highlight some text and drag it in the same way as you would drag an image.

What is a valid drop target?

If the mouse is released over an element that is a valid drop target, that is, one that cancelled the last dragenter or dragover event, then the drop will be successful, and a drop event will fire at the target. Otherwise, the drag operation is cancelled, and no drop event is fired.


1 Answers

According to the drag-and-drop processing model specified in the current (updated June 8, 2021) HTML specification, the drop() event must fire before the dragend() event.

The corresponding information is deeply nested in the document, but the section describing the end of the drag operation looks as follows (omissions and emphasis mine):

Otherwise, if the user ended the drag-and-drop operation (e.g. by releasing the mouse button in a mouse-driven drag-and-drop interface), or if the drag event was canceled, then this will be the last iteration. Run the following steps, then stop the drag-and-drop operation:

  1. [...]
    Otherwise, the drag operation might be a success; run these substeps:
    1. Let dropped be true.
    2. If the current target element is a DOM element, fire a DND event named drop at it; otherwise, use platform-specific conventions for indicating a drop.
    3. [...]
  2. Fire a DND event named dragend at the source node.
  3. [...]
like image 104
Robby Cornelissen Avatar answered Oct 07 '22 23:10

Robby Cornelissen