ユーザーが上部をスクロールすると固定される div で、ナビゲーションとサブ ナビゲーションをラップしたいと考えています。_s スターター テーマを使用しています。私のテーマはテストと呼ばれています。
これがラッパーと一緒の私のナビコードです(最初にこれを理解しようとしているので、サブナビはありません):
<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');
}
});
});
誰かが私が間違ったことを指摘してください。それは単にスクロールし続け、固定されません。
編集:私はこれを使用しています: