gpt4 book ai didi

javascript - 整个页面作为拖放区

转载 作者:可可西里 更新时间:2023-11-01 01:17:09 25 4
gpt4 key购买 nike

在编写接受文件输入的网络应用程序时,我想使用拖放功能,但我不希望页面上只有一个小的放置区。我认为如果您可以放在页面上的任何位置会更方便。幸运的是,window.ondrop 事件会在页面上的任何位置触发,但我想要一些奇特的效果来向用户直观地展示拖放​​是可能的。

要做到这一点,只需要检测文件何时被拖入窗口,以及何时被拖出,以触发显示用户该应用程序启用了拖动。事实证明,拖动事件并不是那么方便。我假设 window.ondragenter 只会在用户进入页面时触发一次。然后当您离开窗口时,它会触发 window.ondragleave。错误的。当鼠标在页面中的子元素上移动时,它会不断触发。

我查看了事件对象中可用的属性,试图找到任何可以隔离我需要的东西,但没有任何效果。我得到的最进一步的是能够更改 body 的背景颜色。并且仅当页面上没有其他内容时。

大量的文件上传网站都做到了。例如 Imgur 和 WeTransfer。他们的网站都是意大利面编码和压缩到不可读的程度,我通过谷歌搜索找不到任何关于这个主题的内容。

那么如何做到这一点呢?

最佳答案

诀窍是使用覆盖整个页面的放置区,并缓存 targetwindow.ondragentertarget 比较的 window.ondragleave .

首先是降落区:

<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:hiddendisplay: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 的目标当您拖出窗口时触发的事件。

为什么会这样?我不知道,但那是怎么做的。这几乎是唯一在用户拖出页面时触发的工作方法。

我相信它是有效的,因为一旦 dropzone 被取消隐藏,它将总是是最后一个目标。它覆盖了页面的每个像素,甚至是 <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 中运行。

关于javascript - 整个页面作为拖放区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28226021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com