Skip to content

Instantly share code, notes, and snippets.

@maxbucknell
Created November 24, 2013 02:41
Show Gist options
  • Save maxbucknell/7622611 to your computer and use it in GitHub Desktop.
Save maxbucknell/7622611 to your computer and use it in GitHub Desktop.
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