Replace small-menu.js with navigation.js inspired by Twenty Twelve.
Doesn't use jQuery, slightly easier to understand, uses a media query for max-width, rather than JS events making it faster to respond.
This commit is contained in:
parent
7fef5cd2ec
commit
22e1613bde
5 changed files with 48 additions and 43 deletions
32
js/navigation.js
Normal file
32
js/navigation.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* navigation.js
|
||||
*
|
||||
* Handles toggling the navigation menu for small screens.
|
||||
*/
|
||||
( function() {
|
||||
var button = document.getElementById( 'site-navigation' ).getElementsByTagName( 'h1' )[0],
|
||||
menu = document.getElementById( 'site-navigation' ).getElementsByTagName( 'ul' )[0];
|
||||
container = document.getElementById( 'site-navigation' );
|
||||
|
||||
if ( undefined == button || undefined == menu )
|
||||
return false;
|
||||
|
||||
button.onclick = function() {
|
||||
if ( -1 == menu.className.indexOf( 'nav-menu' ) )
|
||||
menu.className = 'nav-menu';
|
||||
|
||||
if ( -1 != button.className.indexOf( 'toggled-on' ) ) {
|
||||
button.className = button.className.replace( ' toggled-on', '' );
|
||||
menu.className = menu.className.replace( ' toggled-on', '' );
|
||||
container.className = container.className.replace( ' main-small-navigation', ' main-navigation' );
|
||||
} else {
|
||||
button.className += ' toggled-on';
|
||||
menu.className += ' toggled-on';
|
||||
container.className = container.className.replace( ' main-navigation', ' main-small-navigation' );
|
||||
}
|
||||
};
|
||||
|
||||
// Hide menu toggle button if menu is empty.
|
||||
if ( ! menu.childNodes.length )
|
||||
button.style.display = 'none';
|
||||
} )();
|
|
@ -1,39 +0,0 @@
|
|||
/**
|
||||
* Handles toggling the main navigation menu for small screens.
|
||||
*/
|
||||
jQuery( document ).ready( function( $ ) {
|
||||
var $masthead = $( '#masthead' ),
|
||||
timeout = false;
|
||||
|
||||
$.fn.smallMenu = function() {
|
||||
$masthead.find( '.site-navigation' ).removeClass( 'main-navigation' ).addClass( 'main-small-navigation' );
|
||||
$masthead.find( '.site-navigation h1' ).removeClass( 'assistive-text' ).addClass( 'menu-toggle' );
|
||||
|
||||
$( '.menu-toggle' ).unbind( 'click' ).click( function() {
|
||||
$masthead.find( '.menu' ).toggle();
|
||||
$( this ).toggleClass( 'toggled-on' );
|
||||
} );
|
||||
};
|
||||
|
||||
// Check viewport width on first load.
|
||||
if ( $( window ).width() < 600 )
|
||||
$.fn.smallMenu();
|
||||
|
||||
// Check viewport width when user resizes the browser window.
|
||||
$( window ).resize( function() {
|
||||
var browserWidth = $( window ).width();
|
||||
|
||||
if ( false !== timeout )
|
||||
clearTimeout( timeout );
|
||||
|
||||
timeout = setTimeout( function() {
|
||||
if ( browserWidth < 600 ) {
|
||||
$.fn.smallMenu();
|
||||
} else {
|
||||
$masthead.find( '.site-navigation' ).removeClass( 'main-small-navigation' ).addClass( 'main-navigation' );
|
||||
$masthead.find( '.site-navigation h1' ).removeClass( 'menu-toggle' ).addClass( 'assistive-text' );
|
||||
$masthead.find( '.menu' ).removeAttr( 'style' );
|
||||
}
|
||||
}, 200 );
|
||||
} );
|
||||
} );
|
Loading…
Add table
Add a link
Reference in a new issue