为什么页面刚加载出来的时候就已经滚动到底部了啊
window.onload = function(){
var obtn = document.getElementById('btn');
//获取页面可视区的高度
var clientHeight = document.documentElement.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.ceil(osTop /20);
document.documentElement.scrollTop = document.body.scrollTop = osTop - ispeed;
isTop = true;
console.log(osTop -ispeed);
if (osTop == 0){
clearInterval(timer);
}
},30);
}
}// JavaScript Document