首页 > 其他 > 详细

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

时间:2015-01-07 13:00:57      阅读:243      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!