Forked from scottjehl/delayedEnter, delayedLeave, and delayedHover.js
Last active
September 12, 2017 05:27
-
-
Save ixisio/5098447 to your computer and use it in GitHub Desktop.
Simple delayed events for `mouseenter` & `mouseleave`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* jQuery special events for delayedEnter, delayedLeave, and delayedHover | |
* Author: Scott Jehl, [email protected] | |
* Copyright (c) 2011 Filament Group | |
* licensed under MIT | |
* note: Each event can be used with bind or live event handling as you would use mouseenter,mouseleave, and hover | |
* events fire after 200ms timeout | |
*/ | |
(function($){ | |
//delayedEnter event | |
$.map('delayedEnter delayedLeave'.split(' '), function( eventname ) { | |
$.event.special[ eventname ] = { | |
enabled: true, | |
setup: function() { | |
var thisObject = this, | |
$this = $( thisObject ), | |
timeout = $.fn[ eventname ].delay, | |
hovering, | |
timer; | |
function trigger( event ) { | |
var originalType = event.type; | |
event.type = eventname; | |
// Use dispatch instead of handle since 1.9 | |
$.event.dispatch.call( thisObject, event ); | |
event.type = originalType; | |
} | |
function func1( event ) { | |
if ( !$.event.special[ eventname ].enabled ) { | |
return; | |
} | |
hovering = true; | |
clearTimeout( timer ); | |
timer = setTimeout(function() { | |
if ( hovering ) { | |
trigger( event ); | |
} | |
}, timeout ); | |
} | |
function func2( event ) { | |
hovering = false; | |
} | |
$this.bind({ | |
"mouseenter": ( eventname == "delayedEnter" ? func1 : func2 ), | |
"mouseleave": ( eventname == "delayedEnter" ? func2 : func1 ) | |
}); | |
}, | |
// .off() hook | |
remove: function() { | |
$(this).off('mouseleave mouseenter'); | |
} | |
}; | |
//make available as method | |
$.fn[ eventname ] = function( fn ) { | |
return fn ? this.bind( eventname, fn ) : this.trigger( eventname ); | |
}; | |
$.fn[ eventname ].delay = 200; | |
// Internal attr "attrFn" was removed since 1.9.0 | |
// http://jquery.com/upgrade-guide/1.9/#removed-properties-of-the-event-object | |
// read comments here: https://github.com/jquery/jquery-mobile/pull/4759 | |
if ( $.attrFn ) { | |
$.attrFn[ eventname ] = true; | |
} | |
}); | |
//proxy similar to jQuery's hover | |
//if leaveCallback isn't defined, enterCallback will apply to both | |
$.fn.delayedHover = function( enterCallback, leaveCallback ){ | |
return this.delayedEnter(enterCallback).delayedLeave( leaveCallback || enterCallback ); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment