最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show overlay on 'dragenter' when dragging a file from desktop to the browser - Stack Overflow

programmeradmin3浏览0评论

i'm trying to achieve the same effect as on imgur (drag a file from the desktop to imgur and you'll see a cool overlay). already have a working solution thanks to this post: Event propagation, overlay and drag-and-drop events

BUT: i find the solution rather unsatisfying. the problem is $(document).on('dragenter') gets fired multiple times when hovering over child elements. i was looking for an event that gets fired ONCE when i enter the viewport and ONCE when i leave the viewport so i could have a clean $overlay.fadeIn() and .fadeOut() on dragenter and dragleave.

i solved it with a transparent element that fills the whole viewport. i then call dragenter on that transparent element instead of on $(document). with $('*:visible').live('dragenter') i then show the hidden and the real overlay. $('#transparentOverlay').on('dragleave') hides the overlays. pretty hacky but it works (at least in safari/chrome/firefox)

but just the selector $('*:visible').live() gives me a headache...

anyone have a better suggestion?

i'm trying to achieve the same effect as on imgur. (drag a file from the desktop to imgur. and you'll see a cool overlay). already have a working solution thanks to this post: Event propagation, overlay and drag-and-drop events

BUT: i find the solution rather unsatisfying. the problem is $(document).on('dragenter') gets fired multiple times when hovering over child elements. i was looking for an event that gets fired ONCE when i enter the viewport and ONCE when i leave the viewport so i could have a clean $overlay.fadeIn() and .fadeOut() on dragenter and dragleave.

i solved it with a transparent element that fills the whole viewport. i then call dragenter on that transparent element instead of on $(document). with $('*:visible').live('dragenter') i then show the hidden and the real overlay. $('#transparentOverlay').on('dragleave') hides the overlays. pretty hacky but it works (at least in safari/chrome/firefox)

but just the selector $('*:visible').live() gives me a headache...

anyone have a better suggestion?

Share Improve this question edited May 23, 2017 at 12:10 CommunityBot 11 silver badge asked Feb 9, 2012 at 9:31 Philip PaetzPhilip Paetz 6075 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Try it like this, which works well for me. Essentially, it's imitating the dragenter and dragleave events, but only using dragover:

;(function() {
    var isOver = false, interval;

    $(document).on('dragover', function(e) {
        e.preventDefault();

        clearInterval(interval);

        interval = setInterval(function() {
            isOver = false;
            clearInterval(interval);

            /*** callback for onDragLeave ***/
        }, 100);

        if (!isOver) {
            isOver = true;

            /*** callback for onDragEnter ***/
        }
    });
})();

A more lightweight version of the answer above:

;(function() {
    var dragTimeout;

    $(document).on('dragenter', function(e) {
        // dragenter code
    });

    $(document).on('dragleave', function(e) {
        dragTimeout = setTimeout(function() {
            dragTimeout = undefined;
            // your dragleave code
        });
    });

    $(document).on('dragover', function(e) {
        if (dragTimeout) {
            clearTimeout(dragTimeout);
            dragTimeout = undefined;
        }
    });
})();

May need to see more code/errors you're experiencing. Have you tried a simple boolean to check when your event has fired and limit the subsequent events?

var dragging = false;

$(document).on('dragenter', function(){
    if(!dragging){
        //DO SOMETHING
        dragging = true;
    }
});

$(document).on('dragleave', function(){
    if(dragging){
        //DO SOMETHING
        dragging = false;
    }
});
发布评论

评论列表(0)

  1. 暂无评论