提问人:Jenny 提问时间:11/16/2023 更新时间:11/16/2023 访问量:18
在滚动到部分时,使用相应的锚点将活动类添加到 div
On scroll to section, add active class to div with corresponding anchor
问:
我有一个粘性导航,链接到页面上具有唯一 ID 的锚点。我必须包含完整链接,因为此导航将位于其他页面上。我希望当我滚动到某个部分时,相应的导航链接将添加“活动”类。感谢您收到的任何帮助 我在下面包含了代码,脚本是我一直在使用的东西,但还不完全在那里,但欢迎任何建议。再次感谢!
const changeNav = (entries, observer) => {
entries.forEach((entry) => {
if(entry.isIntersecting && entry.intersectionRatio >= 0.85) {
document.querySelector('.active').classList.remove('active');
// get id of the intersecting section
const id = entry.target.getAttribute('id');
// find matching link & add appropriate class
const newLink = document.querySelector(`[href="#${id}"]`).classList.add('active');
}
});
}
const options = {
threshold: 0.85
}
const observer = new IntersectionObserver(changeNav, options);
// target the elements to be observed
const sections = document.querySelectorAll('section');
sections.forEach((section) => {
observer.observe(section);
});
nav {
position: sticky;
top: 0px;
}
.wrapper {
display: inline-flex;
justify-content: space-between;
background-color: #f4f4f4;
padding-top: 0px;
width: 100%;
}
element.style {
}
.nav-link.active {
color: #aa812d;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="wrapper">
<div class="nav-container">
<nav id="nav">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link" href="http://www.somedomain.com#welcome" onclick="navFunction()">
WELCOME
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.somedomain.com#destination" onclick="navFunction()">
DESTINATION
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.somedomain.com#hotel" onclick="navFunction()">
HOTEL
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.somedomain.com#agenda" onclick="navFunction()">
AGENDA
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.somedomain.com#program" onclick="navFunction()">
PROGRAM
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.somedomain.com#winners" onclick="navFunction()">
WINNERS
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.somedomain.com#travel" onclick="navFunction()">
TRAVEL
</a>
</li>
</ul>
</nav>
</div>
<div class="container">
<section id="welcome" class="section">
<h2>Welcome</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br/><br/>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="destination" class="section">
<h2>Destination</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br/><br/>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="hotel" class="section">
<h2>Hotel</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br/><br/>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="agenda" class="section">
<h2>Agenda</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="program" class="section">
<h2>Program</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br/><br/>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="winners" class="section">
<h2>Winners</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="travel" class="section">
<h2>Travel</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br/><br/>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
</div>
</div>
答: 暂无答案
评论