Skip to content

Instantly share code, notes, and snippets.

@KnightAlex
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save KnightAlex/a1bd50f07fc87df19696 to your computer and use it in GitHub Desktop.

Select an option

Save KnightAlex/a1bd50f07fc87df19696 to your computer and use it in GitHub Desktop.
Toggle mobile menu based on menu going onto 2 lines
//function for showing/hiding mobile menu dynamically
function mobileMenuToggle(){
//get height of menu and auto-detect when it needs to switch to mobile version
var menuHeight = $('#site-navigation').height();
//menu pushed onto 2 lines, hide it and show mobile one
if((isMobile == false) && (menuHeight > 30)){ //EDIT THIS HEIGHT!
//get the current browser width, as once the browser is this width (or wider) again, normal menu will need to kick in.
$('#site-navigation').hide();
$('#mobile').show();
isMobile = true;
browserWidth = $('body').width();
}
if(isMobile == true){
//if browser reaches the fixed size recorded, switch back to normal menu again
if($('body').width() > browserWidth){
$('#site-navigation').show();
$('#mobile').hide();
isMobile = false;
}
}
}
var isMobile = false;//start as false, then check:
mobileMenuToggle();
$(window).resize(function() {
mobileMenuToggle();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment