Skip to content

Commit

Permalink
Making mouseenter and mouseleave work with .live().
Browse files Browse the repository at this point in the history
  • Loading branch information
louisremi authored and jeresig committed Nov 9, 2009
1 parent e03aee1 commit d251809
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/event.js
Expand Up @@ -517,6 +517,14 @@ var withinElement = function( event ) {
// handle event if we actually just moused on to a non sub-element
jQuery.event.handle.apply( this, arguments );
}

},

// In case of event delegation, we only need to rename the event.type,
// liveHandler will take care of the rest.
delegate = function( event ) {
event.type = event.data;
jQuery.event.handle.apply( this, arguments );
};

// Create mouseenter and mouseleave events
Expand All @@ -525,11 +533,11 @@ jQuery.each({
mouseout: "mouseleave"
}, function( orig, fix ) {
jQuery.event.special[ fix ] = {
setup: function(){
jQuery.event.add( this, orig, withinElement, fix );
setup: function(data){
jQuery.event.add( this, orig, data && data.selector ? delegate : withinElement, fix );
},
teardown: function(){
jQuery.event.remove( this, orig, withinElement );
teardown: function(data){
jQuery.event.remove( this, orig, data && data.selector ? delegate : withinElement );
}
};
});
Expand Down Expand Up @@ -743,9 +751,17 @@ function liveHandler( event ) {

jQuery.each( jQuery.data( this, "events" ).live || [], function( i, fn ) {
if ( fn.live === event.type ) {
var elem = jQuery( event.target ).closest( fn.selector, event.currentTarget )[0];
var elem = jQuery( event.target ).closest( fn.selector, event.currentTarget )[0],
related;
if ( elem ) {
elems.push({ elem: elem, fn: fn, closer: jQuery.lastCloser });
// Those two events require additional checking
if ( fn.live === "mouseenter" || fn.live === "mouseleave" ) {
related = jQuery( event.relatedTarget ).closest( fn.selector )[0];
}

if ( !related || related !== elem ) {
elems.push({ elem: elem, fn: fn });
}
}
}
});
Expand Down

0 comments on commit d251809

Please sign in to comment.