固定滚动导航

时间:2013-10-11 22:16:33

标签: scroll fixed nav

我想将导航和子导航包装在一个div中,一旦用户滚动到顶部,它就会被修复。我正在使用_s starter主题。我的主题叫做测试。

这是我的导航代码和包装器(我没有subnav,因为我想先弄清楚这个):

<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 -->

这是我的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%;
}

这是我的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' );

这是我的固定导航文件:

( 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');
}
});
});

如果有人可以请指出我做错了什么。它只是保持滚动而不会被修复。

编辑:我正在使用它:

http://jsfiddle.net/CriddleCraddle/Wj9dD/

1 个答案:

答案 0 :(得分:1)

所以我不是百分百肯定我明白你想要什么,或者什么不适合你。但是,如果您希望nav_bar在向下滚动时保持在顶部,那么您可以正常工作..

您希望nav_bar从顶部开始吗?

这是一个例子,我更改了html,以便更明显地发生了什么。

<div id="banner">
 <h2>put what you want here</h2>
 <p>just adjust javascript size to match this window</p>
</div>

<nav id='nav_bar'>
<ul class='nav_links'>
  <li><a href="url">Nav Bar</a></li>
  <li><a href="url">Sign In</a></li>
  <li><a href="url">Blog</a></li>
  <li><a href="url">About</a></li>
</ul>
</nav>
<div id='body_div'>
  <p style='margin: 0; padding-top: 50px;'>and more stuff to continue scrolling here  </p>
  <br>
  <br>
  <br>
  <br>
  <p>more stuff</p>
  <br>
  <br>
  <br>
  <br>
  <p>and more..</p>
</div>

http://jsfiddle.net/Wj9dD/13/