码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-07 13:04:09      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

<!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>

 

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

标签:

原文地址:http://www.cnblogs.com/mayufo/p/4208100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!