How do I detect a HTML5 drag event entering and leaving the window, like Gmail does?

后端 未结 12 926
醉话见心
醉话见心 2020-12-13 20:16

I\'d like to be able to highlight the drop area as soon as the cursor carrying a file enters the browser window, exactly the way Gmail does it. But I can\'t make it work, an

12条回答
  •  [愿得一人]
    2020-12-13 20:20

    When the file enters and leaves child elements it fires additional dragenter and dragleave so you need to count up and down.

    var count = 0
    
    document.addEventListener("dragenter", function() {
        if (count === 0) {
            setActive()
        }
        count++
    })
    
    document.addEventListener("dragleave", function() {
        count--
        if (count === 0) {
            setInactive()
        }
    })
    
    document.addEventListener("drop", function() {
        if (count > 0) {
            setInactive()
        }
        count = 0
    })
    

提交回复
热议问题