在滚动到部分时,使用相应的锚点将活动类添加到 div

On scroll to section, add active class to div with corresponding anchor

提问人:Jenny 提问时间:11/16/2023 更新时间:11/16/2023 访问量:18

问:

我有一个粘性导航,链接到页面上具有唯一 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>

JavaScript jQuery

评论

0赞 Chris Barr 11/17/2023
指向其他页面的链接是新的 HTML 文档,还是您将使用 JavaScript 更改页面内容。如果是 JavaScript,您只需在链接上添加和删除 CSS 类即可。如果每次都是新的 HTML 文档,则需要在每个页面上手动设置活动类或使用服务器端代码设置活动类。我想你可以用这种方式用JS来做,但是用你使用的任何服务器端语言来做都会容易得多。
0赞 Jenny 11/17/2023
索引页面包含大部分内容,但在单击注册后,用户将被带到几个不同的页面,这些页面也具有导航功能
0赞 Chris Barr 11/17/2023
因此,在其他页面上,您需要让他们设置哪个页面在导航中具有当前活动项目。除非由于某种原因您无权访问该代码或语言

答: 暂无答案