<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title>回到顶部 - 豪情</title>
<style type="text/css">
*{ margin:0; padding:0;}
body{font:12px/1.125 Arial,Helvetica,sans-serif;background:#fff;}
a:focus,input,textarea{outline-style:none;}
a{color:#0a8cd2;text-decoration:none;}
a:hover{text-decoration:underline;}
.none{display:none}
.demo{ height:2000px;}
.gotop{ width:18px;line-height:1.2;padding:7px 0px 7px 2px;background-color:#000;color:#fff;font-size:12px;text-align:center;position:fixed;_position:absolute;right:0px;bottom:100px;_bottom:"auto";cursor:pointer;opacity:0.6;filter:Alpha(opacity=60);border-radius:5px 0 0 5px; display:none;}
.gotop:hover{ text-decoration:none;opacity:0.7;filter:Alpha(opacity=70);}
</style>
</head>
<body>
<div class="demo"></div>
<script type="text/javascript">
(function(){
function viewSize(){
var doc = document,
de = doc.documentElement;
return {
'width': (window.innerWidth || (de && de.clientWidth) || doc.body.clientWidth),
'height': (window.innerHeight || (de && de.clientHeight) || doc.body.clientHeight)
};
}
function getScrollTop(){
return document.documentElement.scrollTop || document.body.scrollTop;
}
var btn = null,
timer = 0;
function apendScroll(){
btn = document.createElement('a');
btn.href = 'javascript:void(0)';
btn.className = 'gotop';
btn.innerHTML = '回到顶部';
document.body.appendChild(btn);
}
apendScroll();
function ie6Top(){
if(!window.XMLHttpRequest){
btn.style.top = getScrollTop() + viewSize().height - 200 + 'px';
}
}
window.onscroll = function(){
btn.style.display = getScrollTop() > 0 ? 'block' : 'none';
ie6Top();
}
btn.onclick = function(){
timer = setInterval(function(){
var scrollTop = getScrollTop();
var iSpeed = Math.floor(- scrollTop / 8);
if(scrollTop == 0){
clearInterval(timer);
}
ie6Top();
document.documentElement.scrollTop = document.body.scrollTop = scrollTop + iSpeed;
}, 30);
}
}());
</script>
</body>
</html>

运行代码