I want to wrap my navigation and sub navigation in a div that becomes fixed once the user scrolls past the top. I'm using _s starter theme. My theme is called test.
Here is my nav code along with the wrapper (I don't have the subnav in, since I'm trying to figure this out first):
<div id='fixed-nav'>
<nav id="site-navigation" class="main-navigation" role="navigation">
<h1 class="menu-toggle"><?php _e( 'Menu', 'test' ); ?></h1>
<div class="skip-link"><a class="screen-reader-text" href="#content"><?php _e( 'Skip to content', 'test' ); ?></a></div>
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
</nav><!-- #site-navigation -->
Here is my css:
#fixed-nav {
border: 0;
background-color: #202020;
border-radius: 0px;
margin-bottom: 0;
height: 30px;
}
.navbar-fixed {
top: 0;
z-index: 100;
position: fixed;
width: 100%;
}
Here's my function.php:
function test_fixed_navigation() {
wp_enqueue_script( 'fixed-navigation', get_template_directory_uri() . '/js/fixed- navigation.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'test_fixed_navigation' );
And here's my fixed-navigation file:
( function() {
$(document).ready(function() {
$(window).scroll(function () {
//if you hard code, then use console
//.log to determine when you want the
//nav bar to stick.
console.log($(window).scrollTop())
if ($(window).scrollTop() > 280) {
$('#fixed-nav').addClass('navbar-fixed');
}
if ($(window).scrollTop() < 281) {
$('#fixed-nav').removeClass('navbar-fixed');
}
});
});
If someone could please point out what I've done wrong. It simply keeps scrolling and doesn't become fixed.
Edit: I'm using this:
http://jsfiddle.net/CriddleCraddle/Wj9dD/