javascript - Javascript querySelector即使在HTML文件末尾也无法与我的脚本一起使用

标签 javascript html css

我正在学习JavaScript,并以以下youtube视频为例:
https://www.youtube.com/watch?v=RLc8NB2JyxE&
他没有提供源代码,但是我已经确保我的代码与视频中的代码完全一样。
本教程介绍了如何使用html,css和javascript制作导航栏,该导航栏在向下滚动屏幕时会发生变化。我可以找到的该错误的任何示例都有“将脚本放在html主体末尾”的解决方案,但是我的已经存在,并且我不确定为什么不加载该脚本。

这是错误:

app.js:28 Uncaught TypeError: Cannot read property 'style' of null
    at app.js:28
    at Array.forEach (<anonymous>)
    at IntersectionObserver.navCheck (app.js:16)
(anonymous) @ app.js:28
navCheck @ app.js:16


index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="./style.css" />
    <title>PhotoGem | slogan here</title>
  </head>
  <body>
    <header>
      <nav>
        <h1>PhotoGem</h1>
        <ul>
          <li><a data-page="home" href="#">Home</a></li>
          <li><a data-page="project" href="#">Project</a></li>
          <li><a data-page="contact" href="#">Contact</a></li>
        </ul>
      </nav>
    </header>
    <main>
      <section data-index="0" class="home">
        <h2>Home</h2>
      </section>
      <section data-index="1" class="project">
        <h2>Projects</h2>
      </section>
      <section data-index="2" class="contact">
        <h2>Contact</h2>
      </section>
    </main>
    <script src="./app.js"></script>
  </body>
</html>


style.css

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
}
header {
  box-shadow: 0px 3px 10px rgba(0, 0, 0, 0.3);
  position: sticky;
  top: 0px;
  background: white;
}
nav {
  min-height: 10vh;
  margin: auto;
  width: 90%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
nav h1,
nav ul {
  font-size: 1.5rem;
  flex: 1;
}
nav ul {
  list-style: none;
  display: flex;
  justify-content: space-between;
}
nav a {
  color: black;
  text-decoration: none;
}
section {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
section h2 {
  font-size: 5rem;
  color: white;
}

.home {
  background: linear-gradient(to right top, #f46b45, #eea849);
}

.project {
  background: linear-gradient(to right top, #005c97, #363795);
}
.contact {
  background: linear-gradient(to right top, #e53935, #e35d5b);
}

.bubble {
  position: absolute;
  z-index: -2;
  background: linear-gradient(to right top orange, yellow);
}


app.js

const sections = document.querySelectorAll("section");
const bubble = document.querySelector(".bubble");
const gradients = [
  "linear-gradients(to right top, #f46b45, #eea849)",
  "linear-gradients(to right top, #005c97, #363795)",
  "linear-gradients(to right top, #e53935, #e35d5b)"
];

const options = {
  threshold: 0.7
};

let observer = new IntersectionObserver(navCheck, options);

function navCheck(entries) {
  entries.forEach(entry => {
    const className = entry.target.className;
    const activeAnchor = document.querySelector(`[data-page=${className}]`);
    const gradientIndex = entry.target.getAttribute("data-index");
    const coords = activeAnchor.getBoundingClientRect();
    const directions = {
      height: coords.height,
      width: coords.width,
      top: coords.top,
      left: coords.left
    };
    if (entry.isIntersecting) {
      bubble.style.setProperty("left", `${directions.left}px`);
      bubble.style.setProperty("top", `${directions.top}px`);
      bubble.style.setProperty("width", `${directions.width}px`);
      bubble.style.setProperty("height", `${directions.height}px`);
    }
  });
}

sections.forEach(section => {
  observer.observe(section);
});

最佳答案

即使我确保div“气泡”在其中(HTML文件),我也遇到同样的问题。

关于javascript - Javascript querySelector即使在HTML文件末尾也无法与我的脚本一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59604831/

相关文章:

html - 在关闭 div 后结束表单,不好的做法?

css - 标签侧边栏向上移动?

javascript - 动态 php 和 html 表单,带有数量、价格和总 JavaScript 计算

javascript - jQuery:将具有一个类的 td 元素添加到 tr

html - 查找 Div 标签下和 span 标签之外的文本的 XPath

html - 出现在 div 中的元素不应该是 : HTML

javascript - javascript 中的全局变量未按预期工作

javascript - 第一个菜单项未被选中

jquery - 将 html 内容加载到 html 页面

html - float 图像在相对位置上不在同一高度