回顶部总结二(动画)

关于回顶部加动画效果

    动画有两种:
一种是CSS动画,需要有样式变化配合transition;
一种是javascript动画,使用定时器来实现 

主要实现javascript动画即定时器

   1.setInterval、
2.setTimeout和
3.requestAnimationFrame这三种可以
setTimeou
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0;}
        #box{height:2000px;overflow: auto;background: red;}
        #top{width:80px;height:80px;line-height: 80px;
            background: #000;opacity: 0.8;color: #fff;text-align: center;font-size: 20px;
            position: fixed;right:10px;bottom:50px;z-index: 2;cursor: pointer;}
        #top:hover{opacity: 1;}
    </style>
</head>
<body>
<div id="box"></div>
<div  id="top"  onclick="totop()">回顶部</div>
<script>
    //解决兼容性
    function getscrolltop(){
        var scrolltop=window.pageXOffset || document.documentElement.scrollTop || document.body.scrollTop;
        return  scrolltop;
    }
   window.onload=function(){
       var top=document.getElementById('top')
       var box=document.getElementById('box')
       top.style.display="none"
       window.onscroll=function(){
           x=getscrolltop();
           if(x>=document.documentElement.clientHeight){
               top.style.display="block";
           }else{
               document.getElementById('top').style.display="none";
           }
       }
   }
    function totop(){
        x-=10;
        document.documentElement.scrollTop=document.body.scrollTop=x;
        jsq=setTimeout(totop,1)
        if(x<=0){
            clearTimeout(jsq)
        }
    }
</script>
</body>
</html>
requestAnimationFrame
    requestAnimationFrame的用法与settimeout相似,只是不需要设置时间间    
 隔,它使用一个回调函数作为参数,这个回调函数会在浏览器重绘之前    
 调用。它返回一个整数,表示定时器的编号,这个值可以传递给cancelAni
 mationFrame用于取消这个函数的执行
requestAnimationFrame scrollTop
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0;}
        #box{height:2000px;overflow: auto;background: red;}
        #top{width:80px;height:80px;line-height: 80px;
            background: #000;opacity: 0.8;color: #fff;text-align: center;font-size: 20px;
            position: fixed;right:10px;bottom:50px;z-index: 2;cursor: pointer;}
        #top:hover{opacity: 1;}
    </style>
</head>
<body>
<div id="box"></div>
<div  id="top"  onclick="totop()">回顶部</div>
<script>
    function getscrolltop(){
        var scrolltop=window.pageXOffset || document.documentElement.scrollTop || document.body.scrollTop;
        return  scrolltop;
    }
    function totop(){
        var timer=requestAnimationFrame(function fun(){
            var x=getscrolltop();
            if(x>0){
                x-=50;
                document.documentElement.scrollTop=document.body.scrollTop=x
                timer=requestAnimationFrame(fun)
            }else{
                cancelRequestAnimationFrame(timer)
            }
        })
    }
</script>
</body>
</html>
requestAnimationFrame scrollTo/scrollBy
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{margin:0;padding:0;}
        #box{height:2000px;overflow: auto;background: red;}
        #top{width:80px;height:80px;line-height: 80px;
            background: #000;opacity: 0.8;color: #fff;text-align: center;font-size: 20px;
            position: fixed;right:10px;bottom:50px;z-index: 2;cursor: pointer;}
        #top:hover{opacity: 1;}
    </style>
</head>
<body>
<div id="box"></div>
<div  id="top"  onclick="toTop()">回顶部</div>
<script>
    function getscrolltop(){
        var scrolltop=window.pageXOffset || document.documentElement.scrollTop || document.body.scrollTop;
        return  scrolltop;
    }
    function toTop(){
        var timer=requestAnimationFrame(function fun(){
            var x=getscrolltop();
            if(x>0){
//                scrollTo(0,x-50);
                scrollBy(0,-50)
                timer=requestAnimationFrame(fun)
            }else{
                cancelRequestAnimationFrame(timer)
            }
        })
        //scrollTo() 方法可把内容滚动到指定的坐标。
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值