Browse Source

mobile nav

windhamdavid 10 years ago
parent
commit
161ca5db5f
2 changed files with 35 additions and 1 deletions
  1. 1 1
      functions.php
  2. 34 0
      js/navigation.js

+ 1 - 1
functions.php

@@ -20,7 +20,7 @@ function dw_scripts() {
 	wp_deregister_script('jquery');
 	wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js', array(), false, true);
 	wp_enqueue_style( 'style', get_stylesheet_uri() );
-	wp_enqueue_script( 'small-menu', get_template_directory_uri() . '/js/small-menu.js', 'jquery', '', true );
+	wp_enqueue_script( 'small-menu', get_template_directory_uri() . '/js/navigation.js', 'jquery', '', true );
 	wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', 'jquery', '', true );
 	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
 		wp_enqueue_script( 'comment-reply', '', '', '', true );

+ 34 - 0
js/navigation.js

@@ -0,0 +1,34 @@
+/**
+ * navigation.js
+ *
+ * Handles toggling the navigation menu for small screens.
+ */
+( function() {
+	var container, button, menu;
+
+	container = document.getElementById( 'site-navigation' );
+	if ( ! container )
+		return;
+
+	button = container.getElementsByTagName( 'button' )[0];
+	if ( 'undefined' === typeof button )
+		return;
+
+	menu = container.getElementsByTagName( 'ul' )[0];
+
+	// Hide menu toggle button if menu is empty and return early.
+	if ( 'undefined' === typeof menu ) {
+		button.style.display = 'none';
+		return;
+	}
+
+	if ( -1 === menu.className.indexOf( 'nav-menu' ) )
+		menu.className += ' nav-menu';
+
+	button.onclick = function() {
+		if ( -1 !== container.className.indexOf( 'toggled' ) )
+			container.className = container.className.replace( ' toggled', '' );
+		else
+			container.className += ' toggled';
+	};
+} )();