动画库 Tweenmax 使用示例2 - 事件和状态

在动画结束后触发onComplete

回调写在to()方法的第三个参数内

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 50px; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            //得到对象
            var t = new TimelineMax();
            //队列动画
            t.to("#div1", 1, {
                left: 300,
                width: 300,
                onComplete: function () {
                    alert("动画执行结束");
                }
            });
        });
    </script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

在反向动画结束后触发onReverseComplete

回调写在to()方法的第三个参数内

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 50px; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            //得到对象
            var t = new TimelineMax();
            //队列动画
            t.to("#div1", 1, {
                left: 300,
                width: 300,
                onReverseComplete: function () {
                    alert("反向动画执行结束");
                }
            });
            //反向播放按钮
            $("#reverse").click(function () {
                t.reverse();
            });
        });
    </script>
</head>
<body>
    <input type="button" id="reverse" value="反向播放" />
    <div id="div1"></div>
</body>
</html>

给动画添加状态(标签)add(),使动画可以分段执行tweenTo()

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 0; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            var t = new TimelineMax();

            //动画队列
            t.to("#div1", 1, { left: 300, width: 300 });
            t.to("#div1", 1, { width: 300 });
                //添加状态1
                t.add("state1");
            t.to("#div1", 1, { height: 300 });
            t.to("#div1", 1, { top: 300 });
                //添加状态2
                t.add("state2");
            t.to("#div1", 1, { rotationZ: 180 });
            t.to("#div1", 1, { opacity: 0 });
                //添加状态3
                t.add("state3");

            //先把动画停止
            t.stop();

            //指定动画执行到状态1
            t.tweenTo("state1");
        });
    </script>

</head>
<body>
    <div id="div1"></div>
</body>
</html>

在动画过程中方便的添加回调add()

add方法不但可以传入一个字符串作为状态标识,也可以传入一个回调函数

这个回调在add之前的动画执行完成后触发

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 0; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            var t = new TimelineMax();

            //动画队列
            t.to("#div1", 1, { left: 300, width: 300 });
            t.to("#div1", 1, { width: 300 });

                //用add方法添加一个回调
                t.add(function () {
                    $("#div1").css("background","pink");
                });

            t.to("#div1", 1, { height: 300 });
            t.to("#div1", 1, { top: 300 });
            t.to("#div1", 1, { rotationZ: 180 });
            t.to("#div1", 1, { opacity: 0 });
        });
    </script>

</head>
<body>
    <div id="div1"></div>
</body>
</html>

运行动画至指定的时间(秒)tweenTo()

add方法不但可以传入一个字符串作为执行到哪个状态,也可以传入一个int

这个int描述将动画执行到第x秒

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 0; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            var t = new TimelineMax();
            //动画队列
            t.to("#div1", 1, { left: 300, width: 300 });
            t.to("#div1", 1, { width: 300 });
            t.to("#div1", 1, { height: 300 });
            t.to("#div1", 1, { top: 300 });
            t.to("#div1", 1, { rotationZ: 180 });
            t.to("#div1", 1, { opacity: 0 });

            //执行动画到第三秒
            t.tweenTo(3);
        });
    </script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

完成动画至指定的时间或状态seek()

tweenTo()是执行动画

seek()则是完成动画,没有过渡效果

seek()到某一状态

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 0; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            var t = new TimelineMax();

            //动画队列
            t.to("#div1", 1, { left: 300, width: 300 });
            t.to("#div1", 1, { width: 300 });
                //添加状态1
                t.add("state1");
            t.to("#div1", 1, { height: 300 });
            t.to("#div1", 1, { top: 300 });
                //添加状态2
                t.add("state2");
            t.to("#div1", 1, { rotationZ: 180 });
            t.to("#div1", 1, { opacity: 0 });
                //添加状态3
                t.add("state3");

            //先把动画停止
            t.stop();

            //指定动画完成到状态2
            t.seek("state2");
        });
    </script>

</head>
<body>
    <div id="div1"></div>
</body>
</html>

seek()到某一时间

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 0; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            var t = new TimelineMax();

            //动画队列
            t.to("#div1", 1, { left: 300, width: 300 });
            t.to("#div1", 1, { width: 300 });
                //添加状态1
                t.add("state1");
            t.to("#div1", 1, { height: 300 });
            t.to("#div1", 1, { top: 300 });
                //添加状态2
                t.add("state2");
            t.to("#div1", 1, { rotationZ: 180 });
            t.to("#div1", 1, { opacity: 0 });
                //添加状态3
                t.add("state3");

            //先把动画停止
            t.stop();

            //指定动画完成到第5秒
            t.seek( 5 );
        });
    </script>

</head>
<body>
    <div id="div1"></div>
</body>
</html>

在seek()下,动画其实也是每一行动画都依次执行了,只是没有过渡效果

seek()第二个参,是否跳过回调函数,默认为ture

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        #div1 { width: 100px; height: 100px; background: red; position: absolute; left: 0; top: 0; }
    </style>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/uncompressed/TweenMax.js"></script>
    <script>
        $(function () {
            var t = new TimelineMax();

            //动画队列
            t.to("#div1", 1, {
                left: 300,
                width: 300,
                onComplete: function myfunction() {
                    alert("执行了回调");
                }
            });
            t.to("#div1", 1, { width: 300 });
                //添加状态1
                t.add("state1");
            t.to("#div1", 1, { height: 300 });
            t.to("#div1", 1, { top: 300 });
                //添加状态2
                t.add("state2");
            t.to("#div1", 1, { rotationZ: 180 });
            t.to("#div1", 1, { opacity: 0 });
                //添加状态3
                t.add("state3");

            //先把动画停止
            t.stop();

            //指定动画完成到state2,且不跳过回调
            t.seek("state2", false);
        });
    </script>

</head>
<body>
    <div id="div1"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值