None of the other answered worked for me. For my case, I needed to raise the drop-target z-index above other elements when dragging started, and lower it back after dragging ended. What I found was that when dragging was initiated, the mousemove
event stopped firing. I used that behavior to detect when the drag actually stopped.
NOTE: only tested w/ firefox v104
let dragging = false;
function dragInit(event) {
// add drag-init class to all drop targets, raising their z-index
if (dragging) return // prevent duplicate calls
dragging = true
for (const dt of document.querySelectorAll('.droptarget')) dt.classList.add('drag-init')
// bind mousemove to body element; mouse move events are stopped while dragging
document.body.addEventListener('mousemove', dragEnd)
}
function dragEnd(event) {
// when the dragging ends, the mousemove events are generated, remove the drag-init class to lower the
// droptargets back below the other elements
dragging = false
// remove the listener, no longer needed
document.body.removeEventListener('mousemove', dragEnd)
for (const dt of document.querySelectorAll('.droptarget')) dt.classList.remove('drag-init')
}
// initialize drag and drop
document.body.addEventListener('dragenter', dragInit)
CSS
.droptarget {
// start the drop target below other elements
z-index: -1;
}
.droptarget.drag-init {
// user is currently dragging, raise the drop target above other elements
z-index: 100;
}