阅读背景:

缓动动画原理--封装基本函数

来源:互联网 

动画速度由快到慢

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
            position: absolute;
            left: 0;
            opacity: 0.3;
        }
    </style>
</head>
<body>
<button id="btn200">200</button>
<button id="btn400">400</button>
<div id="box"></div>
</body>
</html>
<script>
    var btn200 = document.getElementById("btn200");
    var btn400 = document.getElementById("btn400");
    var box=document.getElementById("box");
    btn200.onclick=function(){
      animate(box,200);
    }
    btn400.onclick=function(){
      animate(box,400);
    }
    var timer=null;
    function animate(obj,target){
      obj.timer=setInterval(function(){
        var speed=(target-obj.offsetLeft)/10;
        speed=speed>0?Math.ceil(speed):Math.floor(speed);
        console.log(speed);
        obj.style.left=obj.offsetLeft+speed+"px";
        if(obj.offsetLeft==target){
          clearInterval(obj.timer);
        }
      },50);
    }








</script>
<!DOCTYPE html>
<html>
<head lang



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: