0

Has jQuery an animating-event (not :animated)? Something like step, but as extra method/event?

Currently I trigger a custom event in each animations step-function:

$('.foo').animate({property: 'value'}, {
    step: function(now, fx){
        $(this).trigger('animating', [now, fx])
    }
});

$('.foo').on('animating', function(e, now, fx){
    // do what you want
});

I think there's somewhere in the wild assuredly a better solution. Any ideas/approaches?

Update

I've written a (of course not awesome) special event, what make's handling this problem a bit easier:

(function($){
    var oldStep = $.fx.step._default;
    jQuery.event.special.animating = { };
    $.fx.step._default = function( fx ) {
        $(fx.elem).trigger('animating', fx);
        oldStep.apply( this, arguments );
    };
}(jQuery));

This doesn't work with jQuery 1.7+ since jQuery's animation method was changed, or rather the step function is not longer extendable ($.fx.step results in an empty object).

jQuery.fx refers to Tween.prototype.init; & jQuery.fx.step to {};

So it can't work...

I look still further on every tip!

yckart
  • 32,460
  • 9
  • 122
  • 129
  • 3
    There is nothing built-in, the next best would be to simply do what you are already doing: trigger your own event. – Kevin B Jan 22 '13 at 21:37
  • What you have now seems like overkill for a simple function call `doCustomEvent(e, now, fx);` Do you want a different action to happen on each step? – pierdevara Jan 22 '13 at 22:53
  • @CosminPascu Yes! The best way would if the new (custom)-`event` behaves like the original. – yckart Jan 22 '13 at 23:09
  • What about adding a `switch(stepNr)` inside the `step: function(){}` which would trigger a different function on each step case? Considering you know how many steps your animation goes through. – pierdevara Jan 22 '13 at 23:17
  • @CosminPascu Interesting thought... However that does not solve my problem ;) And by the way, you can get the number of all steps through `fx.start` and `fx.end`... – yckart Jan 23 '13 at 14:18

0 Answers0