导航栏在页面顶部变回透明

时间:2020-02-14 08:21:35

标签: javascript css navbar

我是JavaScript的新手,仍然可以解决问题。

我设法使导航仅出现在顶部和向上滚动。问题是,一旦它位于顶部,我就需要返回透明背景,但是在更改启动后它仍保持灰色。

/* When the user scrolls down, hide the navbar. When the user scrolls up, show the navbar */
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
  var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementById("navbar").style.top = "0";
    document.getElementById("navbar").style.background = "#2b2b2b";
    document.getElementById("totop").style.bottom = "-120px";

  } else {
    document.getElementById("navbar").style.top = "-50px";
    document.getElementById("navbar").style.background = "transparent";
    document.getElementById("totop").style.bottom = "0px";
  }
  prevScrollpos = currentScrollPos;
}

0 个答案:

没有答案