修改了scrollTop的值,不会触发onscroll事件吗?
window.onload = function() {
var obtn = document.getElementById('btn');
var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
var timer = null;
var isTop = true;
window.onscroll = function() {
var osTop = document.documentElement.scrollTop || document.body.scrollTop;
if (osTop >= clientHeight) {
obtn.style.display = 'block';
} else {
obtn.style.display = 'none';
}
if (!isTop) {
clearInterval(timer);
}
isTop = false;
};
obtn.onclick = function() {
//设置定时器
timer = setInterval(function() {
var osTop = document.documentElement.scrollTop || document.body.scrollTop;
var ispeed = Math.floor(-osTop / 6);
document.documentElement.scrollTop = document.body.scrollTop = osTop + ispeed;
isTop = true;
console.log(osTop - ispeed);
if (osTop == 0) {
clearInterval(timer);
}
}, 30);
}
}
在onclick的方法中,修改了scrollTop的值不会触发onscroll事件吗????事实上这段代码能运行成功。我不知道为什么不会,我觉得应该会呀。