简体   繁体   English

获取未捕获的类型错误:无法读取滚动的空错误的属性“offsetTop”

[英]Getting Uncaught TypeError: Cannot read property 'offsetTop' of null error for scroll

I am having an issue where I get a uncaught type error, but the script still works.我遇到了一个问题,我收到了未捕获的类型错误,但脚本仍然有效。 I would like to resolve this but cannot seem to see the issue.我想解决这个问题,但似乎看不到这个问题。 Any help is greatly appreciated!任何帮助是极大的赞赏! Script is below.脚本如下。

StickyNav: function() {
    let mainNavLinks = document.querySelectorAll(".js-pdp-link");
    let mainSections = document.querySelectorAll("section");
    let lastId;
    let cur = [];

    window.addEventListener("scroll", event => {
        let fromTop = window.scrollY + 70;

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

        if (section.offsetTop <= fromTop && section.offsetTop + section.offsetHeight > fromTop) {
            link.classList.add("js-active");
       } else {
           link.classList.remove("js-active");
       }
       });
    });
}

When you do:当你这样做时:

let mainNavLinks = document.querySelectorAll(".js-pdp-link");

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

You are iterating all the elements with the .js-pdp-link class and, if they implement the HTMLHyperlinkElementUtils interface ( ie is an a or an area element), extracting their hash property.您正在使用.js-pdp-link类迭代所有元素,如果它们实现了HTMLHyperlinkElementUtils接口(aarea元素),则提取它们的哈希属性。 Then, you use the hash as a selector.然后,您使用hash作为选择器。

It can fail in three different ways:它可能以三种不同的方式失败:

  1. If the target element doesn't implement HTMLHyperlinkElementUtils , link.hash returns undefined .如果目标元素未实现HTMLHyperlinkElementUtils ,则link.hash返回undefined

  2. If the href attribute contains no hash, link.hash returns an empty string.如果href属性不包含哈希,则link.hash返回一个空字符串。

  3. If there is no element with a id equal to that hash , querySelector(link.hash) is null .如果没有id等于该hash元素,则querySelector(link.hash)null

The case 2 results in SyntaxError: '' is not a valid selector when you try document.querySelector(link.hash) .当您尝试document.querySelector(link.hash)时,情况 2 会导致SyntaxError: '' is not a valid selector

The other two allow you to run the querySelector but result in section being null .另外两个允许您运行querySelector但导致sectionnull Then, if you try to access its .offsetTop property, you will get TypeError: section is null .然后,如果您尝试访问其.offsetTop属性,您将得到TypeError: section is null

暂无
暂无

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

相关问题 未捕获的TypeError:无法读取null的属性&#39;offsetTop&#39; - Uncaught TypeError: Cannot read property 'offsetTop' of null 反应选择:未捕获的类型错误:无法读取 null 的属性“offsetTop” - React-select: Uncaught TypeError: Cannot read property 'offsetTop' of null 未捕获的TypeError:无法读取null的属性“ offsetTop”(导航栏) - Uncaught TypeError: Cannot read property 'offsetTop' of null (navbar) 滚动功能引发错误“未捕获的TypeError:无法读取null的属性&#39;offsetTop&#39;” - Scrolling function throws error “Uncaught TypeError: Cannot read property 'offsetTop' of null” 获取错误为:未捕获的TypeError:无法读取null的属性“ parentNode” - Getting error as : Uncaught TypeError: Cannot read property 'parentNode' of null 得到此错误“未捕获的TypeError:无法读取null的属性&#39;documentElement&#39;” - getting this error “Uncaught TypeError: Cannot read property 'documentElement' of null” 无法读取 null 的属性“offsetTop” - Cannot read property 'offsetTop' of null React Infinity scool:TypeError:无法读取 null 的属性“offsetTop” - React Infinity scrool : TypeError: Cannot read property 'offsetTop' of null 获取“未捕获的 TypeError:无法读取属性 'addEventListener' of null” - Getting a "Uncaught TypeError: Cannot read property 'addEventListener' of null" 捕获TypeError:无法读取null的属性“ getAttribute”,但代码可以正常工作 - Getting Uncaught TypeError: Cannot read property 'getAttribute' of null but code is working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM