Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改页面滚动上突出显示的导航栏按钮(不工作)_Javascript_Jquery_Css_Wordpress_Navbar - Fatal编程技术网

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

Javascript 更改页面滚动上突出显示的导航栏按钮(不工作),javascript,jquery,css,wordpress,navbar,Javascript,Jquery,Css,Wordpress,Navbar,当用户滚动浏览一个页面站点时,我试图改变导航栏,因为每个部分都反映了不同的导航栏按钮,我希望它们在用户滚动到相应页面时突出显示 我已经研究并尝试实施了一些解决方案,这里有很多,但我没有任何运气 我的js代码如下: // Cache selectors var lastId, topMenu = $("#primary-menu"), topMenuHeight = topMenu.outerHeight()+30, // All list items me

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

我已经研究并尝试实施了一些解决方案,这里有很多,但我没有任何运气

我的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");
   }                   
});     
这个脚本来自一个朋友的网站,效果非常好。我不明白为什么它对我的没有任何作用,因为我们的命名约定是相同的

也许这可能是一个css问题

该网站是www.enitrecreative.com/stone


谢谢你的帮助/建议/建议

简单您需要做的是在导航栏中使用scrollspy,在导航栏中指定您要使用的a href,然后您只需将您的部分命名为类似的,就可以了。

您可以向我们展示导航栏的HTML吗?您需要提供更多的上下文。这可能是HTML或CSS问题。javascript中有很多特定于类和id的调用,如果您的样式或HTML不匹配。。。上面的代码无法实现您的期望。
<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 -->
    <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>
.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;
}