bryc bryc - 12 days ago 5
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

window.ondragenter
would trigger only once, when the user entered the page. Then when you left the window, it'd trigger
window.ondragleave
. 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
body
. 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

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:

<style>
div.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;
}
</style>
<!-- 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

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

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

window.addEventListener("dragleave", function(e)
{
    if(e.target === lastTarget)
    {
        document.querySelector(".dropzone").style.visibility = "hidden";
        document.querySelector(".dropzone").style.opacity = 0;
    }
});
</script>

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

Comments