คุณต้องติดตามว่าองค์ประกอบใดที่ dragenter และ dragleave ถูกกระตุ้นเมื่อ การฟัง dragenter และ dragleave ในแต่ละองค์ประกอบจะไม่เพียงบันทึกเหตุการณ์ในองค์ประกอบนั้น แต่ยังรวมถึงเหตุการณ์ในเด็กด้วย
$.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');
}
});