javascript - 在初始化另一个函数或动画之前如何确保动画完成?

标签 javascript function animation raphael settimeout

所以在这里,我有一系列使用 Raphael 的动画:

  1. 淡入曲线
  2. 淡入球 1
  3. 动画球 1
  4. 淡入球 2
  5. 动画球 2

但是,在我的代码中,步骤 4-5 已启动,而步骤 2-3 仍在动画中。如何确保在 1-3 的动画完成后启动步骤 4 和 5?我试过在我的第二个函数(ball2)上使用 setTimeout,但没有成功。

View on JSFiddle或在这里:

Raphael("bounce", 640, 480, function () {
               var r = this,
                p = r.path("M0,77.255c0,0,269.393,37.431,412.96,247.653 c0,0,95.883-149.719,226.632-153.309").attr({stroke: "#666", opacity: 0, "stroke-width": 1}),
                len = p.getTotalLength(),
            e = r.circle(0, 0, 7).attr({stroke: "none", fill: "#000", opacity:0}).onAnimation(function () {
                    var t = this.attr("transform");
                });
             f = r.circle(0, 0, 7).attr({stroke: "none", fill: "#000",opacity:0}).onAnimation(function () {
                    var t = this.attr("transform");
               });
            r.customAttributes.along = function (v) {
                var point = p.getPointAtLength(v * len);
                return {
                    transform: "t" + [point.x, point.y] + "r" + point.alpha
                };
            };
            e.attr({along: 0});
            f.attr({along: 0});

            var rotateAlongThePath = true;
            function fadecurve(ca,ba,aa,ab){
                ca.animate({opacity:1},500);
                setTimeout(function(){fadeball(ba,aa,ab);
                },1000);
            }
            function fadeball(ba,aa,ab) {
                   ba.animate({opacity:1},400);
                   setTimeout(function(){run(ba, aa,ab);
                   },1000);
            }
            function run(ba,aa,ab) {
                   ba.animate({along: aa}, ab, ">", function () {
                    ba.attr({along: aa});
                });
            }
            function startbounce() {
                fadecurve(p,e,.9,400),
                setTimeout(function(){fadeball(f,.8,400);
                    },1000);
            }
            startbounce();
        }); ​

最佳答案

根据 Raphael 的文档,animate 方法采用回调方法作为它的第四个参数。该方法可用于启动序列中的下一个动画(或在第三个动画之后)。

    function fadecurve(ca,ba,aa,ab){
        ca.animate({opacity:1},500,,function(){fadeball(ba,aa,ab);});
    }

例如。

关于javascript - 在初始化另一个函数或动画之前如何确保动画完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10339554/

相关文章:

php - 在 Javascript 函数中使用 PHP

javascript - 函数调用 javascript 中的 "this"

ios - 基于 UIView block 的动画怪异行为

animation - 我可以将动画 SVG 渲染到 Canvas 吗?

Python 定义函数只输出第一个目录

swift - 有什么方法可以判断哪个状态更改导致在 SwiftUI 中重建 View ?

javascript - 调整大小时将 Div 转换为 Accordion 式

javascript - html页面内的输出形式

php - 用于更改 true 与 false 函数值的复选框

c++ - wxPuts 是做什么的?