【问题标题】:How do I ensure an animation is complete before initializing another function or animation?在初始化另一个函数或动画之前,如何确保动画完成?
【发布时间】:2012-04-26 18:49:14
【问题描述】:

所以在这里,我有一系列使用 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();
        }); ​

【问题讨论】:

    标签: javascript function animation raphael settimeout


    【解决方案1】:

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

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

    例如。

    【讨论】:

      猜你喜欢
      • 2014-01-24
      • 2016-11-29
      • 2014-12-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多