简体   繁体   English

滚动错误时的主动导航 - Vanilla JS

[英]Active navigation on scroll error - Vanilla JS

I'm trying to make an active state nav menu were the anchor links are highlighted once they're scrolled to each section.我正在尝试制作一个活动的 state 导航菜单,一旦它们滚动到每个部分,锚链接就会突出显示。 I just tested my site and received the following errors:我刚刚测试了我的网站并收到以下错误:

Uncaught DOMException: Failed to execute 'querySelector' on 'Document': The provided selector is empty.未捕获的 DOMException:无法在“文档”上执行“querySelector”:提供的选择器为空。

NodeList.forEach ()节点列表.forEach()

The errors are for this code:错误是针对此代码的:

navLinks.forEach(link => {
    let section = document.querySelector(link.hash);

I've never seen these before and I'm confused as to what I should do.我以前从未见过这些,我对我应该做什么感到困惑。 Thank you!谢谢!

--HTML--
<div class="menu">
<div class="top-nav">
<div class="logo">
<h1>Store</h1>
</div>
<div class="close">
<i class="bx bx-x"></i>
</div>
</div>
<ul class="nav-list">
<li class="nav-item"><a class="nav-link active" href="#home">Home</a></li>
<li class="nav-item"><a href="#products" class="nav-link">Products</a></li>
<li class="nav-item"><a href="#featured" class="nav-link">Featured</a></li>
<li class="nav-item"><a href="#best-sellers" class="nav-link">Best Sellers</a></li>
<li class="nav-item"><a href="#contact" class="nav-link">Contact</a></li>
<li class="nav-item"><a href="#" class="nav-link icon"><i class="bx bx-shopping-bag"></i></a></li>
</ul>
</div>

<main>
<section class="section featured">
   <div class="title">
      <h1 id="products">Products</h1>
   </div>
</section>

<section class="section featured">
   <div class="title">
      <h1 id="featured">Featured</h1>
   </div>

</section>
<section class="section featured">
   <div class="title">
      <h1 id="best-sellers">Best Sellers</h1>
   </div>
</section>
</main> 

<footer id="footer" class="section footer">
   <div class="footer-title">
      <h1 id="contact">Contact</h1>
   </div>
</footer>

--JS--
//Change navigation style on scroll
window.addEventListener('scroll', event => { 
    let scrollLink = document.querySelector('a.nav-link'); 
    
    (window.scrollY >= 45) ? scrollLink.classList.add('scroll') : scrollLink.classList.remove('scroll');
});

//Active navigation on scroll
window.addEventListener('scroll', event => {
  let navLinks = document.querySelectorAll('a');
  let fromTop = window.scrollY;
 
  navLinks.forEach(link => {
    let section = document.querySelector(link.hash);
   
    if (
      section.offsetTop <= fromTop &&
      section.offsetTop + section.offsetHeight > fromTop
    ) {
      link.classList.add('active');
    } else {
      link.classList.remove('active');
    }
  });
});

--CSS--
a {
    color: inherit;
    text-decoration: none;
}

a:hover,
a:focus {
    color: #fa448c;
    text-decoration: underline;
}

a.active {
    padding: 8px 15px;
    display: block;
    text-transform: capitalize;
    background-color: #fa448c;
    color: #fff;
}

li {
    list-style-type: none;
}

This code means that the number of sections must correspond to the number of tags a .此代码意味着sections的数量必须与标签的数量相对应a

I corrected your code a bit, and also commented out the first and last a tag.我稍微更正了您的代码,并注释掉了第一个和最后a标签。 See how it works.看看它怎么运作。 But there is no need to stop there.但没有必要止步于此。 It is necessary to exclude the first and last tag when accessing, using method querySelectorAll(".nav-list a") .访问时需要排除第一个和最后一个标签,使用方法querySelectorAll(".nav-list a")

 let mainNavLinks = document.querySelectorAll(".nav-list a"); window.addEventListener("scroll", event => { let fromTop = window.scrollY; mainNavLinks.forEach(link => { let section = document.querySelector(link.hash); if ( section.offsetTop <= fromTop && section.offsetTop + section.offsetHeight > fromTop ) { link.classList.add("active"); } else { link.classList.remove("active"); } }); });
 .menu { position: sticky; top: 0; background-color: white; } a { color: inherit; text-decoration: none; } a:hover, a:focus { color: #fa448c; text-decoration: underline; }.active { padding: 8px 15px; display: block; text-transform: capitalize; background-color: #fa448c; color: #fff; } li { list-style-type: none; } #products { background-color: red; height: 500px; } #featured { background-color: green; height: 500px; } #best-sellers { background-color: yellow; height: 500px; } #contact { background-color: grey; height: 500px; }
 --HTML-- <div class="menu"> <div class="top-nav"> <div class="logo"> <h1>Store</h1> </div> <div class="close"> <i class="bx bx-x"></i> </div> </div> <ul class="nav-list"> <,--li class="nav-item"><a class="nav-link active" class="home" href="#home">Home</a></li--> <li class="nav-item"><a href="#products" class="nav-link">Products</a></li> <li class="nav-item"><a href="#featured" class="nav-link">Featured</a></li> <li class="nav-item"><a href="#best-sellers" class="nav-link">Best Sellers</a></li> <li class="nav-item"><a href="#contact" class="nav-link">Contact</a></li> <,--li class="nav-item"><a href="#" class="nav-link icon"><i class="bx bx-shopping-bag"></i></a></li--> </ul> </div> <div class="sections"> <div id="products">"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system. and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness, No one rejects, dislikes, or avoids pleasure itself. because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful, Nor again is there anyone who loves or pursues or desires to obtain pain of itself. because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure, To take a trivial example? which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure,"</div> <div id="featured">"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system. and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness, No one rejects, dislikes, or avoids pleasure itself. because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful, Nor again is there anyone who loves or pursues or desires to obtain pain of itself. because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure, To take a trivial example? which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure,"</div> <div id="best-sellers">"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system. and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness, No one rejects, dislikes, or avoids pleasure itself. because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful, Nor again is there anyone who loves or pursues or desires to obtain pain of itself. because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure, To take a trivial example? which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure,"</div> <div id="contact">"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system. and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness, No one rejects, dislikes, or avoids pleasure itself. because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful, Nor again is there anyone who loves or pursues or desires to obtain pain of itself. because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure, To take a trivial example? which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM