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



You need to track which elements dragenter and dragleave had been triggered on. Listening dragenter and dragleave on an individual element will capture not only events on that element but also events on children.

$.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');
         }
      });
   });
};

Listen for events −

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

Advertisements