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:
Konstantin Kovshenin 2012-09-03 21:45:28 +04:00
parent 7fef5cd2ec
commit 22e1613bde
5 changed files with 48 additions and 43 deletions

View File

@ -102,7 +102,7 @@ add_action( 'widgets_init', '_s_widgets_init' );
function _s_scripts() { function _s_scripts() {
wp_enqueue_style( 'style', get_stylesheet_uri() ); wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_script( 'small-menu', get_template_directory_uri() . '/js/small-menu.js', array( 'jquery' ), '20120206', true ); wp_enqueue_script( 'navigation', get_template_directory_uri() . '/js/navigation.js', null, '20120206', true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' ); wp_enqueue_script( 'comment-reply' );

View File

@ -51,8 +51,8 @@
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2> <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
</hgroup> </hgroup>
<nav role="navigation" class="site-navigation main-navigation"> <nav role="navigation" class="site-navigation main-navigation" id="site-navigation">
<h1 class="assistive-text"><?php _e( 'Menu', '_s' ); ?></h1> <h1 class="menu-toggle"><?php _e( 'Menu', '_s' ); ?></h1>
<div class="assistive-text skip-link"><a href="#content" title="<?php esc_attr_e( 'Skip to content', '_s' ); ?>"><?php _e( 'Skip to content', '_s' ); ?></a></div> <div class="assistive-text skip-link"><a href="#content" title="<?php esc_attr_e( 'Skip to content', '_s' ); ?>"><?php _e( 'Skip to content', '_s' ); ?></a></div>
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?> <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

32
js/navigation.js Normal file
View 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';
} )();

View File

@ -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 );
} );
} );

View File

@ -405,12 +405,24 @@ a:active {
/* Small menu */ /* Small menu */
.menu-toggle { .menu-toggle {
display: none;
cursor: pointer; cursor: pointer;
} }
.main-small-navigation .menu {
.main-small-navigation ul {
display: none; display: none;
} }
@media screen and (max-width: 600px) {
.menu-toggle,
.main-small-navigation ul.nav-menu.toggled-on {
display: block;
}
.main-navigation ul {
display: none;
}
}
/* =Content /* =Content
----------------------------------------------- */ ----------------------------------------------- */