在创建一个粘滞的导航栏时,我注意到一个奇怪的错误,该导航栏应该在滚动上设置动画,降低其高度.
当滚动较小时(例如,只在向下箭头键上轻击一次),导航就会反弹,因为脚本似乎返回0表示滚动.
以下是代码:
window.onscroll = function() {
var scrollDiv = document.getElementById('dojo-nav');
if (window.scrollY > 0) {
scrollDiv.style.height = '50px';
} else {
scrollDiv.style.height = '200px';
}
};
body {
margin: 0;
}
#dojo-nav {
transition: all 0.2s ease-in;
}
<div id="dojo-nav" style="position:sticky;top:0px;background: violet;height: 200px;"></div>
<div style="height:50vh;background-color: cyan;"></div>
<div style="height:100vh;background-color: orange;"></div>
有没有人可以在不设置固定位置和导航杆间隔或相同高度的情况下解决这个问题?