首页 > 解决方案 > 未捕获的类型错误:无法读取 chrome 未注册的 null 属性“classList”(试图显示和消失标题)

问题描述

IDK 有什么问题,但是 chrome 说即使我的所有设置都完全按照视频所描绘的那样设置,但无法读取分类器 PS:我是 Coding 的初学者,所以 IDK 为什么它没有被 chrome 读取。

我希望发生的是添加一个标题,然后使标题出现和消失,但它在 Codepen 上完美运行,但不是 chrome 你能解释为什么吗?


  var doc = document.documentElement;
  var w = window;

  var prevScroll = w.scrollY || doc.scrollTop;
  var curScroll;
  var direction = 0;
  var prevDirection = 0;

  var Header = document.getElementById('site-header');

  var checkScroll = function() {

    /*
    ** Find the direction of scroll
    ** 0 - initial, 1 - up, 2 - down
    */

    curScroll = w.scrollY || doc.scrollTop;
    if (curScroll > prevScroll) { 
      //scrolled up
      direction = 2;
    }
    else if (curScroll < prevScroll) { 
      //scrolled down
      direction = 1;
    }

    if (direction !== prevDirection) {
      toggleHeader(direction, curScroll);
    }
    
    prevScroll = curScroll;
  };

  var toggleHeader = function(direction, curScroll) {
    if (direction === 2 && curScroll > 92) { 
      
      //replace 52 with the height of your header in px

      Header.classList.add("hide");
      prevDirection = direction;
    }
    else if (direction === 1) {
      Header.classList.remove("hide");
      prevDirection = direction;
    }
  };
  
  window.addEventListener('scroll', checkScroll);

})();```

标签: javascript

解决方案


推荐阅读