windows监听滚动事件,滚动满足fixed条件时,元素一直抖动是什么原因?
window.addEventListener('scroll', this.handleTabFix, true)
handleTabFix () {
let timeOut = null
clearTimeout(timeOut)
timeOut = setTimeout(() => {
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
let offsetTop = document.querySelector('#testNavBar') && document.querySelector('#testNavBar').offsetTop + 60
scrollTop > offsetTop ? this.isFixTab = true : this.isFixTab = false
// isFixTab为true时,设置元素为fixed
}, 1000)
}
猜测是你设置某个元素
fixed
之后,页面高度就不够了;然后它就又被解除fixed
,然后页面高度又超了;于是反复。解决方案,有两个:
position: sticky
fixed
之后留着壳撑页面