Skip to content

Instantly share code, notes, and snippets.

@searleb
Created March 10, 2016 00:16
Show Gist options
  • Save searleb/2ac57a0c4d30283fb114 to your computer and use it in GitHub Desktop.
Save searleb/2ac57a0c4d30283fb114 to your computer and use it in GitHub Desktop.
Click any where that isn't the nav to close but clicking anywhere on the nav won't close it. The one is also staggered closing multiple nav levels.
/**
* Closes all navigation layers, staggered from bottom
* if the user clicks anywhere outside of the nav
*/
$('html').on('click', function(event) {
// if the closest parent is not the nav
if (!$(event.target).closest('nav').length) {
// get all elements with the nav--open class and reverse the array
$($('.nav--open').get().reverse()).each(function(i, el) {
// remove the element nav--open class with 300ms * i delay to get the stagger effect
(function(i){
setTimeout(function(){
$(el).removeClass('nav--open');
}, 300 * i);
}(i));
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment