HTML5 dragleave fired when hovering a child element HTML5 dragleave fired when hovering a child element jquery jquery

HTML5 dragleave fired when hovering a child element


You just need to keep a reference counter, increment it when you get a dragenter, decrement when you get a dragleave. When the counter is at 0 - remove the class.

var counter = 0;$('#drop').bind({    dragenter: function(ev) {        ev.preventDefault(); // needed for IE        counter++;        $(this).addClass('red');    },    dragleave: function() {        counter--;        if (counter === 0) {             $(this).removeClass('red');        }    }});

Note: In the drop event, reset counter to zero, and clear the added class.

You can run it here


Is it possible to prevent dragleave from firing when dragging into a child element?

Yes.

#drop * {pointer-events: none;}

That CSS seem to be enough for Chrome.

While using it with Firefox, the #drop shouldn't have text nodes directly (else there's a strange issue where a element "leave it to itself"), so I suggest to leave it with only one element (e.g., use a div inside #drop to put everything inside)

Here's a jsfiddle solving the original question (broken) example.

I've also made a simplified version forked from the @Theodore Brown example, but based only in this CSS.

Not all browsers have this CSS implemented, though:http://caniuse.com/pointer-events

Seeing the Facebook source code I could find this pointer-events: none; several times, however it's probably used together with graceful degradation fallbacks. At least it's so simple and solves the problem for a lot of environments.


It has been quite some time after this question is asked and a lot of solutions (including ugly hacks) are provided.

I managed to fix the same problem I had recently thanks to the answer in this answer and thought it may be helpful to someone who comes through to this page.The whole idea is to store the evenet.target in ondrageenter everytime it is called on any of the parent or child elements. Then in ondragleave check if the current target (event.target) is equal to the object you stored in ondragenter.

The only case these two are matched is when your drag is leaving the browser window.

The reason that this works fine is when the mouse leaves an element (say el1) and enters another element (say el2), first the el2.ondragenter is called and then el1.ondragleave. Only when the drag is leaving/entering the browser window, event.target will be '' in both el2.ondragenter and el1.ondragleave.

Here is my working sample. I have tested it on IE9+, Chrome, Firefox and Safari.

(function() {    var bodyEl = document.body;    var flupDiv = document.getElementById('file-drop-area');    flupDiv.onclick = function(event){        console.log('HEy! some one clicked me!');    };    var enterTarget = null;    document.ondragenter = function(event) {        console.log('on drag enter: ' + event.target.id);        enterTarget = event.target;        event.stopPropagation();        event.preventDefault();        flupDiv.className = 'flup-drag-on-top';        return false;    };    document.ondragleave = function(event) {        console.log('on drag leave: currentTarget: ' + event.target.id + ', old target: ' + enterTarget.id);        //Only if the two target are equal it means the drag has left the window        if (enterTarget == event.target){            event.stopPropagation();            event.preventDefault();            flupDiv.className = 'flup-no-drag';                 }    };    document.ondrop = function(event) {        console.log('on drop: ' + event.target.id);        event.stopPropagation();        event.preventDefault();        flupDiv.className = 'flup-no-drag';        return false;    };})();

And here is a simple html page:

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Multiple File Uploader</title><link rel="stylesheet" href="my.css" /></head><body id="bodyDiv">    <div id="cntnr" class="flup-container">        <div id="file-drop-area" class="flup-no-drag">blah blah</div>    </div>    <script src="my.js"></script></body></html>

With proper styling what I have done is to make the inner div (#file-drop-area) much bigger whenever a file is dragged into the screen so that the user can easily drop the files into the proper place.