问题遇到的现象和发生背景
用window.onscoll实现滑动固定顶部功能,出现刚滑动就会执行的问题
window.onscroll = function () {
if (document.documentElement.scrollTop || document.body.scrollTop > 2000) {
totop.style.transition = 'height 1s'
totop.style.height = '100px'
}
else {
totop.style.transition = 'height 0s'
totop.style.height = 0
}
}
但是下面又可以
window.onscroll = function () {
var t = document.documentElement.scrollTop || document.body.scrollTop
if (t > 2000) {
totop.style.transition = 'height 1s'
totop.style.height = '100px'
}
else {
totop.style.transition = 'height 0s'
totop.style.height = 0
}
}
这是为什么啊??