I'm trying to implement a drag/drop uploader that marks the dropzone with an overlay (100% width / height absolute element inside the dropzone, looks great when static) when a file is dragged into the window, and removes that marking when the file leaves the window or is dropped outside the dropzone.
The issue is that when the file is dragged into the window, dragover and dragleave events fire like crazy, and the overlay thus flashes in and out like crazy.
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
}
dragenter
instead ofdragover
? – ChristiandragLeave
is fired when the element becomes visible. I also have the problem and tried it with adding styles instead of adding and removing the overlay element, but with no luck. – ohcibi