Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ตรวจจับ Dragleave Event ใน Firefox เมื่อลากออกนอกหน้าต่าง

ปัญหา: dragleave ไม่ยิงเมื่อลากออกนอกหน้าต่างใน Firefox

ต้องติดตาม element ที่รับ dragenter และ dragleave เอง

jQuery Plugin: draghover

$.fn.draghover = function(options) {
    return this.each(function() {
        var collection = $(), self = $(this);
        self.on('dragenter', function(ev) {
            if (collection.length === 0) self.trigger('draghoverstart');
            collection = collection.add(ev.target);
        });
        self.on('dragleave drop', function(ev) {
            collection = collection.not(ev.target);
            if (collection.length === 0) self.trigger('draghoverend');
        });
    });
};

การใช้งาน

$(window).draghover().on({
    'draghoverstart': function() { alert('dragged into the window'); },
    'draghoverend': function() { alert('dragged out of window'); }
});

นับจำนวน elements ที่กำลังถูกลากอยู่ เมื่อเป็น 0 ถือว่าลากออกนอกพื้นที่แล้ว