How do I detect a file is being dragged rather than a draggable element on my page?

后端 未结 4 1679
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 01:30

I\'m using the html5 events to enable both file and element drag-and-drop. I\'ve attached the dragover event to the body and am using event delegations to show where a dragg

4条回答
  •  旧时难觅i
    2020-12-29 02:24

    Further improvement of bouke's answer:

    Since chrome calls dragleave of document on every dragenter foe each element, it can cause flickering of the dropzone, especially if there are many nested elements.

    $(document).on('dragleave', function(e) {
        dragTimer = window.setTimeout(function() {
            $("#dropzone").hide();
            }, 25);
    });
    

    What I did to fix the issue for me is increasing the timeout a bit and adding clearTimeout before setting each timeout, since previously in some cases there would be more than one timeouts which are not cleared in the dragover event, since dragTimer stores only the latest one. The result version:

    $(document).on('dragleave', function(e) {
        window.clearTimeout(dragTimer);
        dragTimer = window.setTimeout(function() {
            $("#dropzone").hide();
        }, 85);
    });
    

    btw, thanks for the idea! My other solution was an absolute pain :)

提交回复
热议问题