我正在尝试实现一个拖放上传器,当将文件拖到窗口中时,它会使用叠加层标记放置区域(放置区域内的 100% 宽度/高度绝对元素,静态时看起来很棒),并在文件离开窗口或被拖放到放置区之外。
问题是当文件被拖入窗口时,dragover 和 dragleave 事件会像疯了一样触发,因此覆盖层会像疯了一样闪进闪出。
window.addEventListener('dragover', handleDrag, false);
window.addEventListener('dragleave', handleStop, false);
window.addEventListener('drop', handleStop, false);
dropzone.addEventListener('drop', handleUpload, false);
function handleDrag(event) {
// Stop normal browser response.
event.stopPropagation();
event.preventDefault();
if (!window.mysettings.dragging) {
window.mysettings.dragging = true;
$('#dropzone').prepend('<div class="overlay">HELLO</div>');
}
}
function handleStop(event) {
// Stop normal browser response.
event.stopPropagation();
event.preventDefault();
if (window.mysettings.dragging) {
window.mysettings.dragging = false;
$('#dropzone .overlay').remove();
}
}
function handleUpload(event) {
// Stop normal browser response.
event.stopPropagation();
event.preventDefault();
if (window.mysettings.dragging) {
window.mysettings.dragging = false;
$('#dropzone .overlay').remove();
}
// DO MY FILE UPLOAD STUFF HERE
}