Skip to content

Instantly share code, notes, and snippets.

@sausman
Created October 26, 2014 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sausman/b9c2563acd0a0b4b96b0 to your computer and use it in GitHub Desktop.
Save sausman/b9c2563acd0a0b4b96b0 to your computer and use it in GitHub Desktop.
// Store $('body') and $('.mainMenu') in variables outside of the event handler
// This way they only need to be looked up once and not on every click
var $body = $('body');
var $mainMenu = $(".mainMenu");
$body.click(function(e){
// Since you're only checking the className you can use `hasClass` here instead of `is`
// http://jsperf.com/jquery-is-vs-hasclass
if ( $(e.target).hasClass("js-menuToggle") ) {
$mainMenu.toggleClass("mainMenu--visible");
$body.toggleClass('js-fixScroll');
}
else {
$mainMenu.removeClass("mainMenu--visible");
$body.removeClass('js-fixScroll');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment