Skip to content

Instantly share code, notes, and snippets.

@advitum
Last active September 28, 2016 12:21
Show Gist options
  • Save advitum/7b56f60ebc14a632ae4e8b0ed3523414 to your computer and use it in GitHub Desktop.
Save advitum/7b56f60ebc14a632ae4e8b0ed3523414 to your computer and use it in GitHub Desktop.
You know how it is sometimes very hard to use a multi-level website navigation because it immediately closes when you accidentally move your mouse out of the navigation element and you have to start over? This script just adds a "lingering" class to the element, so your navigation can wait half a second before closing again.
$element.on('mouseenter', function(event) {
window.clearTimeout($(this).data('hover-timeout'));
$(this).addClass('hover').siblings('.hover').removeClass('hover');
}).on('mouseleave', function(event) {
(function($element) {
$element.data('hover-timeout', window.setTimeout(function() {
$element.removeClass('hover');
}, 500));
})($(this));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment