更新答案:
#dropzone{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10; //set this to make it sit on the top of everything
pointer-events: none;
}
.user-is-dragging #dropzone{
pointer-events: all !important;
}
//element declarations
const dropzone = document.getElementById("dropzone");
const body = document.body;
//timeout function to help detect when the user is dragging something
let dragHandle;
// utility function to detect drag & drop support
function dragDropSupported() {
var div = document.createElement('div');
return ('draggable' in div) || ('ondragstart' in div && 'ondrop' in div);
}
function initDragDrop(){
//simply exit / do other stuff if drag & drop is not supported
if(!dragDropSupported()){
console.warn("Drag & drop not supported");
return;
}
//add user-is-dragging class which enables pointer events for the drop event
body.addEventListener("dragover", (e) => {
body.classList.add("user-is-dragging");
clearTimeout(dragHandle);
dragHandle = setTimeout(() => {
body.classList.remove("user-is-dragging");
}, 200);
});
//this is to prevent the browser from opening the dragged file(s)
dropzone.addEventListener('dragover', (e) => {
e.preventDefault();
});
dropzone.addEventListener('drop', (e) => {
//prevent the browser from opening the dragged file(s)
e.preventDefault();
//dragged files
const files = e.dataTransfer.files;
console.log(files);
})
}