<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body{
height: 2000px;
}
#btn{
width: 60px;
height: 60px;
background: #ccc;
position: absolute;
right: 20px;
cursor: pointer;
}
</style>
<script type="text/javascript">
window.onload=function(){
var oBtn=document.getElementById("btn");
var timer=null;
var b;
setTop();
window.onscroll=function(){
if (b!=1) {
clearInterval(timer);
}
setTop();
b=2;
}
oBtn.onclick=function(){
clearInterval(timer);
var iCur=iSpeed=0;
timer=setInterval(function(){
iCur=document.documentElement.scrollTop || document.body.scrollTop;
iSpeed=Math.floor( ( 0 - iCur )/8 );
if (iCur==0) {
clearInterval(timer);
}else{
document.documentElement.scrollTop = document.body.scrollTop = iCur + iSpeed;
}
b=1;
},30)
}
function setTop(){
var scrollTop=document.documentElement.scrollTop || document.body.scrollTop;
oBtn.style.top=scrollTop + document.documentElement.clientHeight-oBtn.offsetHeight-20+'px';
}
}
</script>
</head>
<body>
<div id="btn"></div>
</body>
</html>