JavaScript运动框架(五):链式运动到完美运动

基于前几篇的运动框架基础,本文主要讲解一下链式运动,就是运动完后接着再运动,比如很多网站中,一个方框的出现和退出:出现时先变宽再变高,退出时先变矮再变窄退出!
之前的模型是:

startMove(obj, json);

现在改为:

startMove(obj, json, fn);

也就是在第一次运动结束的时候执行fn(); fn是传过来的一个参数,这个参数是个函数,定时器清理之后手动运行fn();如果想采用链式运动,那就是在fn中再调用startMove(obj, json, fn),再在里面的fn中调用startMove(obj, json, fn),可以一直玩下去


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>运动框架(5五):链式运动到完美运动</title>
    <style type="text/css">
        div {
            width: 100px;
            height: 100px;
            background: orange;
            margin: 10px;
            float: left;
        }
    </style>
</head>
<body>
    <div id="div1"></div>

    <script type="text/javascript">
        var oDiv = document.getElementById('div1');
        oDiv.onmouseover = function() {
            startMove(oDiv, {width:300,opacity:30}, function() {
                startMove(oDiv, {height:500});
            });
        };
        oDiv.onmouseout = function() {
            startMove(oDiv, {height:100}, function() {
                startMove(oDiv, {width:100,opacity:100});
            })
        };

        function getStyle(obj, attr) {
            if (obj.currentStyle) {
                return obj.currentStyle[attr];
            } else {
                return getComputedStyle(obj, null)[attr];
            }
        }

        function startMove(obj, json, fn) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function() {
                var bStop = true;
                for (var attr in json) {
                    var cur = 0;
                    if (attr === 'opacity') {
                        cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
                    } else {
                        cur = parseInt(getStyle(obj, attr));
                    }
                    if (cur != json[attr]) {
                        bStop = false;
                    }
                    var speed = (json[attr] - cur)/10;
                    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                    cur += speed;
                    if (attr === 'opacity') {
                        obj.style.filter = 'alpha(opacity:' + cur + ')';
                        obj.style.opacity = cur/100;
                    } else {
                        obj.style[attr] = cur + 'px';
                    }

                }
                if (bStop) {
                    clearInterval(obj.timer);
                    if (fn) fn();
                }

            }, 30);
        }
    </script>
</body>
</html>

最后提取出来的完美运动框架如下,motionFrame.js:

function getStyle(obj, attr) {
    if (obj.currentStyle) {
        return obj.currentStyle[attr];
    } else {
        return getComputedStyle(obj, null)[attr];
    }
}

function startMove(obj, json, fn) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function() {
        var bStop = true;
        for (var attr in json) {
            var cur = 0;
            if (attr === 'opacity') {
                cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
            } else {
                cur = parseInt(getStyle(obj, attr));
            }
            if (cur != json[attr]) {
                bStop = false;
            }
            var speed = (json[attr] - cur)/10;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
            cur += speed;
            if (attr === 'opacity') {
                obj.style.filter = 'alpha(opacity:' + cur + ')';
                obj.style.opacity = cur/100;
            } else {
                obj.style[attr] = cur + 'px';
            }

        }
        if (bStop) {
            clearInterval(obj.timer);
            if (fn) fn();
        }

    }, 30);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值