JavaScript封装动画函数(一)

简单的动画函数的封装

封装一个从当前位置移动到目标位置的动画

封装一个均速动画函数

        function animate(element,target){
            //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器
            clearInterval(element.timeId);
            element.timeId=setInterval(function () {
                //获取div的当前位置
                var current=element.offsetLeft;
                //设置每次移动多少像素
                var step=10;
                step= current < target ?step:-step;
                //每次移动后的位置
                current += step;
                //判断当前位置距离目标位置多少像素
                if(Math.abs(current-target)>=Math.abs(step)){
                    element.style.left=current+"px";
                }else{
                    clearInterval(element.timeId);
                    element.style.left=target+"px";
                }
            },10);
        }

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        button{
            margin-top: 20px;
        }
        #box{
            width: 200px;
            height: 100px;
            background-color: greenyellow;
            margin-top: 20px;
            position:absolute;
            left: 20px;
        }
    </style>
</head>
<body>
    <button id="btn1">移动到400px</button>
    <button id="btn2">移动到800px</button>
    <div id="box"></div>
    <script>
        var box=document.getElementById("box");
        document.getElementById("btn1").onclick=function () {
            animate(box,400);
        };
        document.getElementById("btn2").onclick=function () {
            animate(box,800);
        };
        function animate(element,target){
            //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器
            clearInterval(element.timeId);
            element.timeId=setInterval(function () {
                //获取div的当前位置
                var current=element.offsetLeft;
                //设置每次移动多少像素
                var step=10;
                step= current < target ?step:-step;
                //每次移动后的位置
                current += step;
                //判断当前位置距离目标位置多少像素
                if(Math.abs(current-target)>=Math.abs(step)){
                    element.style.left=current+"px";
                }else{
                    clearInterval(element.timeId);
                    element.style.left=target+"px";
                }
            },10);
        }
    </script>
</body>
</html>

封装一个变速(缓动)动画函数

   function animate(element,target){
        //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器
        clearInterval(element.timeId);
        element.timeId=setInterval(function () {
            //获取div的当前位置
            var current=element.offsetLeft;
            //设置每次移动多少像素
            var step=(target-current)/10;
            step= step>0 ?Math.ceil(step):Math.floor(step);
            //每次移动后的位置
            current += step;
            element.style.left=current+"px";
            //判断如果到达目标位置
            if(current==target){
                //清理定时器
                clearInterval(element.timeId);
            }
        },10);
    }

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        button{
            margin-top: 20px;
        }
        #box{
            width: 200px;
            height: 100px;
            background-color: greenyellow;
            margin-top: 20px;
            position:absolute;
            left: 20px;
        }
    </style>
</head>
<body>
<button id="btn1">移动到400px</button>
<button id="btn2">移动到800px</button>
<div id="box"></div>
<script>
    var box=document.getElementById("box");
    document.getElementById("btn1").onclick=function () {
        animate(box,400);
    };
    document.getElementById("btn2").onclick=function () {
        animate(box,800);
    };
    function animate(element,target){
        //先清理定时器,这样可以保证每次点击按钮都只产生一个定时器
        clearInterval(element.timeId);
        element.timeId=setInterval(function () {
            //获取div的当前位置
            var current=element.offsetLeft;
            //设置每次移动多少像素
            var step=(target-current)/10;
            step= step>0 ?Math.ceil(step):Math.floor(step);
            //每次移动后的位置
            current += step;
            element.style.left=current+"px";
            //判断如果到达目标位置
            if(current==target){
                //清理定时器
                clearInterval(element.timeId);
            }
        //    测试代码
            console.log("当前位置:"+current+",目标位置:"+target+",每次移动像素数:"+step);
        },10);
    }
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/sleepwalker_1992/article/details/80787873
今日推荐