封装匀速动画

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #box {
            width: 100px;
            height: 100px;
            background-color: lightcoral;
            position: absolute;
            margin-top: 30px;
            left: 0px;

        }
    </style>
</head>
<body>
<button id="btn1">点击移动到400px</button>
<button id="btn2">点击移动到800px</button>
<div id="box"></div>
<script src="common.js"></script>
<script>
    my$("btn2").onclick = function () {
        //只需要产生一个定时器, 可以先清理定时器, 再设置
        clearInterval(this.timeId);
        this.timeId = setInterval(function () {
            //1.获取div当前的位置--->看成盒子当前的left值
            var current = my$("box").offsetLeft;//400
            //2.div每一次移动多少像素 = 10 即步数
            var step = 10;
            //3.每次移动后的距离
            current += step;
            //4.判断移动后的位置是否达到目标位置
            if(current <= 800){
                my$("box").style.left = current + "px";
            }else{
                //清理定时器
                clearInterval(this.timeId);
            }

        },30)

    }
    //封装匀速动画函数--->任意一个元素移动到指定的目标位置
    //@params : 元素  目标位置
    function animate(element,target ) {
        //先清理定时器
        clearInterval(element.timeId)
        element.timeId = setInterval(function () {
            //1.获取div当前的位置--->看成盒子当前的left值
            var current = element.offsetLeft;//number类型
            //2.div每一次移动多少像素 = 10 即步数
            var step = 7;
            //2.1判断往哪边移动
            step = current <= target ? step : -step;
            //3.每次移动后的距离
            current += step;
            ////4.判断剩余的步数是否大于 step
           if(Math.abs(target - current) > Math.abs(step)){
               element.style.left = current + "px";
           }else{
               clearInterval(element.timeId);
               element.style.left = target + "px";

           }
        },20)
    }

    my$("btn1").onclick = function () {
        animate(my$("box"), 400);
    }
    my$("btn2").onclick = function () {
        animate(my$("box"), 800);
    }



</script>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #box {
            width: 100px;
            height: 100px;
            background-color: lightcoral;
            position: absolute;
            margin-top: 30px;
            left: 0px;

        }
    </style>
</head>
<body>
<button id="btn1">点击移动到400px</button>
<button id="btn2">点击移动到800px</button>
<div id="box"></div>
<script src="common.js"></script>
<script>
    my$("btn2").onclick = function () {
        //只需要产生一个定时器, 可以先清理定时器, 再设置
        clearInterval(this.timeId);
        this.timeId = setInterval(function () {
            //1.获取div当前的位置--->看成盒子当前的left值
            var current = my$("box").offsetLeft;//400
            //2.div每一次移动多少像素 = 10 即步数
            var step = 10;
            //3.每次移动后的距离
            current += step;
            //4.判断移动后的位置是否达到目标位置
            if(current <= 800){
                my$("box").style.left = current + "px";
            }else{
                //清理定时器
                clearInterval(this.timeId);
            }

        },30)

    }
    //封装匀速动画函数--->任意一个元素移动到指定的目标位置
    //@params : 元素  目标位置
    function animate(element,target ) {
        //先清理定时器
        clearInterval(element.timeId)
        element.timeId = setInterval(function () {
            //1.获取div当前的位置--->看成盒子当前的left值
            var current = element.offsetLeft;//number类型
            //2.div每一次移动多少像素 = 10 即步数
            var step = 7;
            //2.1判断往哪边移动
            step = current <= target ? step : -step;
            //3.每次移动后的距离
            current += step;
            ////4.判断剩余的步数是否大于 step
           if(Math.abs(target - current) > Math.abs(step)){
               element.style.left = current + "px";
           }else{
               clearInterval(element.timeId);
               element.style.left = target + "px";

           }
        },20)
    }

    my$("btn1").onclick = function () {
        animate(my$("box"), 400);
    }
    my$("btn2").onclick = function () {
        animate(my$("box"), 800);
    }



</script>
</body>
</html>

 

转载于:https://www.cnblogs.com/Ironman725/p/10928047.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值