gpt4 book ai didi

javascript - 可滚动的网页导航

转载 作者:行者123 更新时间:2023-11-28 07:45:24 25 4
gpt4 key购买 nike

我制作了一个可滚动的网页。我想要做的是在按下导航按钮以及查看内容时突出显示导航按钮。当我向下滚动更多时,必须触发导航中的下一个按钮。这是我的代码:

.container {
width: 100%;
height: 100px;
position: relative;
font-family: 'Trebuchet Ms';
}

.bg {
background: #000;
width: 100%;
height: 100px;
opacity: 0;
}

.show {
opacity: 0.6;
}

.transition {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}

.fixed {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 1;
}

ul {
height: 200px;
margin: -70px auto 0 auto;
width: 810px;
}

li {
float: left;
list-style: none;
margin: 10px 20px;
text-transform: uppercase;
letter-spacing: 4px;
color: #000;
}

a {
text-align: center;
font-size: 15px;
color: #CFB547;
font-weight: bold;
text-decoration: none;
letter-spacing: 5px;

position: relative;
z-index: 1;
margin: 0 auto;
display: block;
}

a:hover {
color: #a6a6a6;
text-shadow: 1px 1px 1px 1px;
text-decoration: none;
}

.down {
top: 150px;
}

.up {
top: 1800px;
}
<section id="top">
<div id="top" class="container">
<div class="fixed">
<div class="bg transition">
</div>
<ul>
<li><a href="#top">Crystal Beach</a></li>
<li><a href="#about">Apartments</a></li>
<li><a href="#blanktwo">Facilities</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</div>
</section>

$(window).scroll(function() {
// 100 = The point you would like to fade the nav in.

if ($(window).scrollTop() > 100 ){

$('.bg').addClass('show');

} else {

$('.bg').removeClass('show');

};
});

$('.scroll').on('click', function(e){
e.preventDefault()

$('html, body').animate({
scrollTop : $(this.hash).offset().top
}, 1500);
});

最佳答案

给你https://jsfiddle.net/mekwall/up4nu/

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

// Bind click handler to menu items
// so we can get a fancy scroll animation
menuItems.click(function(e){
var href = $(this).attr("href"),
offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});

// 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");
}
});

关于javascript - 可滚动的网页导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30720933/

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