您需要跟踪哪些元素的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'); } });