JavaScript-简单的动画函数封装

可以控制元素left top width 发生变化时候 的动画

// 根据样式名, 获得样式值  兼容所有浏览器
function getStyle(obj, name) {
    if (window.getComputedStyle) {
        return getComputedStyle(obj, null)[name];
    } else {
        return obj.currentStyle[name];
    }
}

// 简单动画函数
/*
    1. obj 要执行动画的对象
    2. attr要修改动画的样式 left top width...
    3. target 执行动画的目标
    4.speed 移动的速度
    5. callback动画执行完毕以后执行回调函数
*/
function move(obj, attr, target, speed, callback) {
    clearInterval(obj.timer);
    var current = parseInt(getStyle(obj, attr));
    if (current > target) {
        speed = -speed;
    }
    obj.timer = setInterval(function () {
        var oldValue = parseInt(getStyle(obj, attr));
        var newValue = oldValue + speed;
        if ((speed < 0 && newValue < target) || (speed > 0 && newValue > target)) {
            newValue = target;
        }
        obj.style[attr] = newValue + "px";
        if (newValue == target) {
            clearInterval(obj.timer);
            callback && callback();
        }
    }, 30)
}
  • 使用
	<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="js/tools.js"></script>
    <title>Document</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }

        #test {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            top: 40px;
            left: 0px;
        }

        #test2 {
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;
            top: 200px;
            left: 0px;
        }
    </style>

</head>

<body>
    <button id="btn01">向右移动</button>
    <button id="btn02">向左移动</button>
    <button id="btn03">让test2往右边移动</button>
    <button id="btn04">改变test2</button>
    <div id="test">
        test1
    </div>

    <div id="test2">
        test2
    </div>

    <div style="width:0px; height:400px; border-left:1px solid black;position:absolute; left:800px;top:0px;">

    </div>

    <script>
        window.onload = function () {

            var btn01 = document.getElementById("btn01");
            var btn02 = document.getElementById("btn02");
            var btn03 = document.getElementById("btn03");
            var btn04 = document.getElementById("btn04");
            var test = document.getElementById("test");
            var test2 = document.getElementById("test2");

            btn01.onclick = function () {
                move(test,"left",800, 20);
            }
            btn02.onclick = function () {
                move(test,"left", 0, 10);
            }
            btn03.onclick = function () {
                move(test2,"left", 800, 10);
            }
            btn04.onclick = function () {
                move(test2,"width", 300, 10,function(){
                    move(test2,"height", 300, 10,function(){
                        alert("结束");
                    });
                });
            }



        }
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值