Skip to content

Instantly share code, notes, and snippets.

@bradpotter
Last active October 31, 2017 22:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradpotter/8643502 to your computer and use it in GitHub Desktop.
Save bradpotter/8643502 to your computer and use it in GitHub Desktop.
Combine Header, Primary and Secondary Menu into one Responsive Menu
/* Make sure your Menus are named Header Navigation, Primary Navigation and Secondary Navigation in Appearance > Menus */
(function( window, $, undefined ) {
'use strict';
$('.nav-primary').before('<button class="menu-toggle" role="button" aria-pressed="false"></button>'); // Add toggles to menus
$('nav .sub-menu').before('<button class="sub-menu-toggle" role="button" aria-pressed="false"></button>'); // Add toggles to sub menus
// Show/hide the navigation
$('.menu-toggle, .sub-menu-toggle').click(function() {
if ($(this).attr('aria-pressed') == 'false' ) {
$(this).attr('aria-pressed', 'true' );
}
else {
$(this).attr('aria-pressed', 'false' );
}
$(this).toggleClass('activated');
$(this).next('nav, .sub-menu').slideToggle('fast', function() {
return false;
// Animation complete.
});
});
var isMobile = window.innerWidth <= 768; // set the variable
isMobile = !isMobile; // force the first resize
$(window).resize(function() {
if(window.innerWidth > 768 && isMobile) {
isMobile = false; // this IS NOT a mobile size
$("#menu-header-navigation").appendTo("nav.nav-header");
$("#menu-secondary-navigation").appendTo("nav.nav-secondary .wrap");
$("header section.widget_nav_menu").removeAttr("style");
$(".nav-secondary").removeAttr("style");
}
else if(window.innerWidth <= 768 && !isMobile) {
isMobile = true; // this IS a mobile size
$("#menu-header-navigation").insertBefore("#menu-primary-navigation");
$("#menu-secondary-navigation").insertAfter("#menu-primary-navigation");
$("header section.widget_nav_menu").css("display", "none");
$(".nav-secondary").css("display", "none");
}
});
// check the size when its adjusted...
$(window).trigger('resize');
})( this, jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment