更改页面滚动上突出显示的导航栏按钮(不工作)

Change highlighted navbar buttons on page scroll (not working)

本文关键字:按钮 导航 工作 显示 滚动      更新时间:2023-09-26

当用户滚动一个页面网站时,我试图实现导航栏的更改,因为每个部分都反映了不同的导航栏按钮,我希望当用户滚动到相应的页面时,它们会突出显示,

我已经研究并尝试实现了一些解决方案,(这里有很多),但我运气不好。

我的js代码在这里:

    // Cache selectors
var lastId,
    topMenu = $("#primary-menu"),
    topMenuHeight = topMenu.outerHeight()+30,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = 300 + menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    }) ;

// Bind to scroll
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop()+topMenuHeight;
   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";
   if (lastId !== id) {
       lastId = id;
       // Set/remove active class
       menuItems
         .parent().removeClass("active")
         .end().filter("[href=#"+id+"]").parent().addClass("active");
   }                   
});     

来自header.php的代码-声明导航栏

<div id="navbar" class="navbar">
            <nav id="site-navigation" class="navigation main-navigation" role="navigation">
                <button class="menu-toggle"><?php _e( 'Menu', 'twentythirteen' ); ?></button>
                <a class="screen-reader-text skip-link" href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentythirteen' ); ?>"><?php _e( 'Skip to content', 'twentythirteen' ); ?></a>
                <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu_id' => 'primary-menu' ) ); ?>
                <?php get_search_form(); ?>
            </nav><!-- #site-navigation -->
        </div><!-- #navbar -->

page-home.php中的代码-部分示例:

    <div class="section6" id="connect">
<div class = "heading1">connect</div>
<div class="contactform">
<?php if( function_exists( 'ninja_forms_display_form' ) ){ ninja_forms_display_form( 6 ); } ?>
</div>
</div>

以及相关的css代码:

.main-navigation {
text-align:right;
clear: both;
margin: 0 auto;
max-width: 1080px;
min-height: 75px;
position: relative;
  background-image:url("images/logo-white.png");
background-size:70px auto;
background-repeat:no-repeat;
background-position:10px 15px;
}
ul.nav-menu,
div.nav-menu > ul {
line-height: 75px;
margin: 0;
padding: 0 40px 0 0;
}
.nav-menu li {
display: inline-block;
position: relative;
}
.nav-menu li a {
color: #737373;
display: block;
font-size: 10px;
line-height: 1;
padding: 15px 20px;
text-decoration: none;
text-transform:uppercase;
letter-spacing:3px;
outline:none;
}

.nav-menu li.active,
.nav-menu li.active > a {
color: #FCFBD9;
}
.navmenu li a .active {
color: #FCFBD9;
}
.nav-menu li:hover > a,
.nav-menu li a:hover,
.nav-menu li:focus > a,
.nav-menu li a:focus {
color: #FCFBD9;
}

这个脚本来自一个朋友的网站,效果非常好。我不明白为什么它对我的没有任何作用,因为我们的命名约定是相同的。。

也许是css问题?

该网站为www.enitreveryve.com/stone

感谢您的帮助/建议/建议!

简单地说,你需要做的是使用scrollspy,所以在你指定a href的导航中,你会使用say#about,然后你只需像这样命名你的部分,它就会起作用。