简体   繁体   English

粘性导航在第一次滚动时向上滑动不平滑。 好之后

[英]Sticky nav slide up not smooth on first scroll. Fine after

This is driving me mad and I can't find the cause.这让我发疯,我找不到原因。 I'm using the following to make my nav slide up on scroll down and back on scroll up.我正在使用以下内容使我的导航在向下滚动时向上滑动并在向上滚动时返回。

var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
  var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementById("navbar").style.top = "0";
  } else {
    document.getElementById("navbar").style.top = "-140px";
  }
  prevScrollpos = currentScrollPos;
} 

Here's the CSS这是 CSS

#navbar {
    position: fixed;
    width: 100vw;
    z-index: 99;
    transition: all 0.3s ease-in-out;
    overflow: hidden;
}

The problem is that on the first scroll down after page load it just disappears abruptly.问题是在页面加载后第一次向下滚动时,它突然消失了。 It's only on subsequent scrolls that it slides smoothly.只有在随后的滚动中,它才能平滑滑动。

It was that I hadn't specified the initial position.是我没有指定初始位置。 I'll definitely be going back to rework it based on the advice given.我肯定会根据给出的建议重新修改它。

Thanks again.再次感谢。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM