Bạn cần theo dõi phần tử nào đã kích hoạt dragenter và dragleave. Lắng nghe dragenter/dragleave trên phần tử riêng lẻ sẽ bắt cả sự kiện trên phần tử con.
$.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'); } }); }); };
Lắng nghe sự kiện −
$(window).draghover().on({ 'draghoverstart': function() { alert(‘đã kéo vào cửa sổ'); }, 'draghoverend': function() { alert('đã kéo ra khỏi cửa sổ'); } });