Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My take on a hoverIntent plugin for jQuery.
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