bryc bryc - 1 year ago 80
CSS Question

Entire page as a dropzone for drag and drop

While writing web apps that took file input, I wanted to use drag 'n' drop, but I didn't want just a small dropzone on the page. I thought it would be more convenient if you could drop anywhere on the page. Luckily, the window.ondrop event fires anywhere on the page, but I wanted some fancy effect to show the user visually that drag/drop was possible.

To do that, all that was needed was detect when a file was dragged into the window, and when it was dragged out, to trigger an effect that showed the user that the app was drag-enabled. Turns out that drag events are not that convenient. I assumed that

would trigger only once, when the user entered the page. Then when you left the window, it'd trigger
. Wrong. It's constantly firing as the mouse moves over child elements in the page.

I looked at what properties were available in the event object, trying to find anything that could isolate what I needed, but nothing worked. The furtherest I got was being able to change the background color of
. And only if there was nothing else on the page.

Tons of file upload sites got it right. Imgur and WeTransfer for example. Their sites were all spahetti-coded and compressed to the point of unreadability, and I couldn't find anything on the subject by googling.

So how can this be done?

Answer Source

The trick is to use a dropzone covering the entire page, and caching the target of window.ondragenter to compare with the target of window.ondragleave.

First, the dropzone:

    /* positions to point 0,0 - required for z-index */
    position: fixed; top: 0; left: 0; 
    /* above all elements, even if z-index is used elsewhere */
    z-index: 9999999999;               
    /* takes up 100% of page */
    width: 100%; height: 100%;         
    /* 50% black background when visible */
    background-color: rgba(0,0,0,0.5);
    /* visibility toggles after 175ms, opacity will animate for 175ms  */
    transition: visibility 175ms, opacity 175ms;
<!-- both visibility:hidden and display:none can be used,
     but the former can be used in CSS animations -->
<div style="visibility:hidden; opacity:0" class="dropzone"></div>

Even though the dropzone will be covering the entire page, using visibility:hidden or display:none will hide it from view. I used visibility:hidden so that CSS animations can be used to animate the transition.

Assigning the events

/* lastTarget is set first on dragenter, then
   compared with during dragleave. */
var lastTarget = null;

window.addEventListener("dragenter", function(e)
    lastTarget =;
    document.querySelector(".dropzone").style.visibility = "";
    document.querySelector(".dropzone").style.opacity = 1;

window.addEventListener("dragleave", function(e)
    if( === lastTarget)
        document.querySelector(".dropzone").style.visibility = "hidden";
        document.querySelector(".dropzone").style.opacity = 0;

So here's the process: You drag a file over the window, and window.ondragenter immediately fires. The target is set to the root element, <html>. Then you immediately unhide your dropzone, which covers the entire page. window.ondragenter will fire again, this time the target being your dropzone. Each time the dragenter event fires, it will cache the target, because this will be the target that will match the last window.ondragleave event that fires when you drag out of the window.

Why does this work? I have no idea, but that is how to do it. This is pretty much the only working method that triggers when the user drags off the page.

Here's a JSFiddle of it in action. Tested in Chrome, Firefox, and IE11.
After JSFiddle's UI update, it has some issues with the technique, but Codepen shows the effect properly

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download