诀窍是使用覆盖整个页面的拖放区,并缓存window.ondragenter 的target 以与window.ondragleave 的target 进行比较。
首先,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
it can be lowered as needed, but this value surpasses
all elements when used on YouTube for example. */
z-index: 9999999999;
/* takes up 100% of page */
width: 100%; height: 100%;
/* dim the page with 50% black background when visible */
background-color: rgba(0,0,0,0.5);
/* a nice fade effect, visibility toggles after 175ms, opacity will animate for 175ms. note display:none cannot be animated. */
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>
即使放置区将覆盖整个页面,使用visibility:hidden 或display:none 也会将其隐藏在视图之外。我使用了visibility:hidden,以便可以使用 CSS 动画来制作过渡动画。
分配事件
<script>
/* lastTarget is set first on dragenter, then
compared with during dragleave. */
var lastTarget = null;
window.addEventListener("dragenter", function(e)
{
lastTarget = e.target; // cache the last target here
// unhide our dropzone overlay
document.querySelector(".dropzone").style.visibility = "";
document.querySelector(".dropzone").style.opacity = 1;
});
window.addEventListener("dragleave", function(e)
{
// this is the magic part. when leaving the window,
// e.target happens to be exactly what we want: what we cached
// at the start, the dropzone we dragged into.
// so..if dragleave target matches our cache, we hide the dropzone.
// `e.target === document` is a workaround for Firefox 57
if(e.target === lastTarget || e.target === document)
{
document.querySelector(".dropzone").style.visibility = "hidden";
document.querySelector(".dropzone").style.opacity = 0;
}
});
</script>
下面是这个过程:你将一个文件拖到窗口上,window.ondragenter 会立即触发。 target 设置为根元素<html>。然后您立即取消隐藏覆盖整个页面的放置区。 window.ondragenter 将再次开火,这次目标是您的放置区。每次dragenter 事件触发时,它都会缓存目标,因为这将是与您拖出窗口时触发的最后一个window.ondragleave 事件匹配的目标。
为什么会这样?我不知道,但那是怎么做的。这几乎是用户拖出页面时触发的唯一工作方法。
我相信它会起作用,因为一旦放置区被取消隐藏,它将始终成为最后一个目标。它涵盖了页面的每个像素,甚至包括<html> 标签。此方法在离开窗口时依赖于 dragleave 触发。 不幸的是,有一个bug in Firefox 阻止它正常工作。请为它投票,以便它尽快得到修复。从 Firefox 57.0.2 开始,dragleave 似乎可以正常触发。但是,需要一种解决方法,检查 document 而不是缓存的元素:
if(e.target === lastTarget || e.target === document)
Here's a JSBin of it in action。已在最新的 Chrome、Firefox、Edge 和 IE11 中测试。