How to detect the dragleave event in Firefox when dragging outside the window

后端 未结 5 1666
借酒劲吻你
借酒劲吻你 2020-12-04 10:31

Firefox doesn\'t properly trigger the dragleave event when dragging outside of the window:

https://bugzilla.mozilla.org/show_bug.cgi?id=665704

https://bugzil

5条回答
  •  春和景丽
    2020-12-04 11:22

    Inspired by @PhilipWalton 's code, I simplified the jQuery plugin code.

    $.fn.draghover = function(fnIn, fnOut) {
        return this.each(function() {
            var n = 0;
            $(this).on('dragenter', function(e) {
                (++n, n==1) && fnIn && fnIn.call(this, e);
            }).on('dragleave drop', function(e) {
                (--n, n==0) && fnOut && fnOut.call(this, e);
            });
        });
    };
    

    Now you can use the jquery plugin like jquery hover method:

    // Testing code 1
    $(window).draghover(function() {
        console.log('into window');
    }, function() {
        console.log('out of window');
    });
    
    // Testing code 2
    $('#d1').draghover(function() {
        console.log('into #d1');
    }, function() {
        console.log('out of #d1');
    });
    

提交回复
热议问题