dom 回到顶部(兼容IE FF Chrome)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
 #wrap{
     height:2000px;
     background:pink;
     position:relative;
     }
 #ontop{
     position:absolute;
     bottom:0px;
     right:0px;
     background:red;
     color:#FFF;
     font-weight:bold;
     height:100px;
     width:100px;
     line-height:100px;
     text-align:center;
     cursor:pointer;
     }     
</style>
<script type="text/javascript">
window.onload = function ()
{
    var otop = document.getElementById('ontop');
    var timer = null;
    
    otop.onclick = function ()
    {
        timer = setInterval(function()
        {
            document.body.scrollTop -= 50;
            document.documentElement.scrollTop -= 50;
            if(document.body.scrollTop == 0 && document.documentElement.scrollTop == 0 )
            {
                clearInterval(timer);
            }
            
        },20)
        
        
        
        
    };
};
</script>
</head>

<body>
<div id="wrap">
    <span id="ontop">回到顶部</span>
</div>
</body>
</html>

 

posted @ 2015-01-07 12:07  mayufo  阅读(251)  评论(0编辑  收藏  举报