gpt4 book ai didi

javascript - 更改页面滚动时突出显示的导航栏按钮(不起作用)

转载 作者:行者123 更新时间:2023-11-28 06:56:19 26 4
gpt4 key购买 nike

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

我已经查看并尝试实现了一些解决方案,(这里堆满了)但我没有任何运气。

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

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

也许这可能是一个 css 问题?

网站是 www.enitrecreative.com/stone

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

最佳答案

您需要做的很简单,就是使用 scrollspy,因此在您指定 href 的导航中,您可以使用 say #about,然后您只需将您的部分命名为 about 就可以了。

关于javascript - 更改页面滚动时突出显示的导航栏按钮(不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com