首页 > 代码库 > javascript 返回顶部
javascript 返回顶部
<style>
#linGoTopBtn {
POSITION: fixed; TEXT-ALIGN: center; LINE-HEIGHT: 30px; WIDTH: 30px; BOTTOM: 35px; HEIGHT: 33px; FONT-SIZE: 12px; CURSOR: pointer; RIGHT: 0px;
}
</style>
<script>
function linGoTop()
{
var obj = document.getElementById("linGoTopBtn");
function getScrollTop()
{
return document.documentElement.scrollTop;
}
function setScrollTop(value)
{
document.documentElement.scrollTop ? document.documentElement.scrollTop = value : document.body.scrollTop = value;
}
window.onscroll = function()
{
getScrollTop() > 0 ? obj.style.display =‘‘ : obj.style.display = ‘none‘;
}
obj.onclick = function()
{
var goTop = setInterval(scrollMove, 5); // 单位毫秒
function scrollMove()
{
setScrollTop(getScrollTop()/1.1);
if(getScrollTop() < 1) clearInterval(goTop);
}
}
}
</script>
</head>
<body style="text-align:center">
<DIV style="DISPLAY: none" id="linGoTopBtn"><IMG border=0 src="http://www.mamicode.com/images/top.jpg"></DIV>
<SCRIPT type="text/javascript">linGoTop();</SCRIPT>
</body>
</html>