為什么頁面剛加載出來的時候就已經(jīng)滾動到底部了啊
window.onload = function(){
? ? var obtn = document.getElementById('btn');
//獲取頁面可視區(qū)的高度
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(){
//設(shè)置定時器
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
2016-05-13
我用的是火狐,你的代碼在我電腦上是沒問題的,你再試試