隐藏导航栏滚动时不隐藏,我希望它在滚动 javascript 时切换

hide nav bar dont hide when scrolling and i want it toggle when i scroll by javascript

我输入的所有内容都正确,但不起作用。

var prevScrollpos = window.pageYOffset;
window.onscroll = function () {
  var currentScrollpos = window.pageYOffset;
  if (prevScrollpos > currentScrollpos) {
    document.getElementsByClassName("nav").style.top = "0";
  } else {
    document.getElementsByClassName("nav").style.top = "-100px";
  }
  prevScrollpos = currentScrollpos;
};

请重试此代码。应该是document.getElementsByClassName("nav")[0]

    var prevScrollpos = window.pageYOffset;
    window.onscroll = function () {
      var currentScrollpos = window.pageYOffset;
      if (prevScrollpos > currentScrollpos) {
        document.getElementsByClassName("nav")[0].style.top = "0";
      } else {
        document.getElementsByClassName("nav")[0].style.top = "-100px";
      }
      prevScrollpos = currentScrollpos;
    };