Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dragover vs dragenter events of HTML5 drag & drop

I want to know the difference of dragover and dragenter events of HTML5 drag &drop.

I'm not clear about the difference.

like image 609
Never Say Never Avatar asked Sep 20 '15 07:09

Never Say Never


People also ask

What is the name of the event in HTML5 when we drag an item into a valid drop point?

The ondrop event occurs when a draggable element or text selection is dropped on a valid drop target. Drag and drop is a very common feature in HTML5. It is when you "grab" an object and drag it to a different location.

What is drag and drop effect in HTML5?

Drag and Drop (DnD) is powerful User Interface concept which makes it easy to copy, reorder and deletion of items with the help of mouse clicks. This allows the user to click and hold the mouse button down over an element, drag it to another location, and release the mouse button to drop the element there.

Which mouse event is used when handling a drag drop event?

The DragEvent interface is a DOM event that represents a drag and drop interaction. The user initiates a drag by placing a pointer device (such as a mouse) on the touch surface and then dragging the pointer to a new location (such as another DOM element).


2 Answers

The dragenter event happens at the moment you drag something in to the target element, and then it stops. The dragover event happens during the time you are dragging something until you drop it.

Look here:

$('.dropzone').on("dragover", function (event) {
    console.log('dragover');
});
$('.dropzone').on("dragenter", function (event) {
    console.log('dragenter');
});

Now see the console:

enter image description here

As you can see the dragenter happen once (when you drag element in to the target).

But the dragover event happen every few hundred milliseconds.

The same difference exist between drag and dragstart, dragstart happen once but drag happen every few hundred milliseconds.

like image 176
cool Avatar answered Oct 17 '22 09:10

cool


Based on the dragenter and dragover MDN doc...

dragover

The dragover event is fired when an element or text selection is being dragged over a valid drop target (every few hundred milliseconds).

dragenter

The dragenter event is fired when a dragged element or text selection enters a valid drop target.

The dragover is triggered after a small delay (every 350 ms, I think) while the cursor stays over the element.

like image 20
ScaisEdge Avatar answered Oct 17 '22 10:10

ScaisEdge