My take on a hoverIntent plugin for jQuery.
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(function ($) { | |
$.event.special.hoverIntent = (function () { | |
var hoverIntentDelay = 200; | |
return { | |
'setup': function (data, namespaces) { | |
var namespace, | |
$el; | |
namespace = namespaces.length ? '.' + namespaces.join('.'): ''; | |
$el = $(this); | |
$el.on('mouseenter.hoverIntent' + namespace, (function () { | |
var timeoutID; | |
return function () { | |
window.setTimeout(function () { | |
$el.trigger('hoverIntent' + namespace); | |
}, hoverIntentDelay); | |
$el.one('mouseleave', function () { | |
window.clearTimeout(timeoutID); | |
}); | |
}; | |
})()); | |
}, | |
'teardown': function (namespaces) { | |
var namespace, | |
$el; | |
namespace = namespaces.length ? '.' + namespaces.join('.'): ''; | |
$el = $(this); | |
$el.off('mousenter.hoverIntent' + namespace); | |
} | |
}; | |
})(); | |
$.fn.hoverIntent = function (fnOver, fnOut) { | |
return this.on('hoverIntent', fnOver).on('mouseleave', fnOut || fnOver); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment