views:

42

answers:

1

Hi, im building an animation framework for my work, and im stock in the Queue or chain effects part, actually i have something like this:

var Fx = {
    animate: function(){...},
    fadeIn: function(){...},
    fadeOut: function(){...}
}

etc etc... so, actually i can do:

$('#element').animate({options}).fadeIn({options});

and it works excellent! but the fadeIn and the animate execute at the same time but what i want to do, is something like:

$('#element').chain().animate({options}).fadeIn({options});

so it execute the animate first and then the fadeIn

actually i have something like:

var Chain = function(element){
 var target = element;
 for (methodName in Fx) {

  (function(methodName) {
    Chain.prototype[methodName] = function() {
     var args = Array.prototype.slice.call(arguments);
    return this;
    };
  })(methodName);
 }
}

Fx.chain = function(element){
  return 
    }

and i can get all the methods called and that stuff, but i dont know how to push that to an array or even call the first method, because im trying to get all requests to an array and just call it everytime if effects is done.

im not use jQuery, as i said i need to make a personalized framework.

Any idea pleeeasse??! Thank you

+1  A: 

Simple Demo

var Fx = {
  animate: function(el, style, duration, after){
    // do animation...
    after();
  },
  fadeIn: function(el, duration, after){
    // do fading ...
    after();
  }, 
  // other effects ...

  chain: function (el) {

    var que = [];
    function callNext() { que.length && que.shift()(); }

    return {
      animate: function(style, duration) {
        que.push(function() {
          Fx.animate(el, style, duration, callNext);
        });
        return this;
      },
      fadeIn: function(duration){
        que.push(function() {
          Fx.fadeIn(el, duration, callNext);
        });
        return this;
      }, // ...
      end: callNext
    };
  }
};

Usage

Fx.chain(el).animate("style", 300).fadeIn(600).animate("style2", 900).end();
galambalazs
Amazing, gonna try it out and i'll let you know, thank you!!!
Javis Perez
I've updated the *Demo* to see it in action.
galambalazs
+1 Nice answer; wish I could give extra points for providing a demo!
Wouter van Nifterick
I tryied, but doesnt seems to work :( it just execute the first method, i mean, in here: chain(el).animate(opts).fadeOut(opts); it just does the animate part, not the fadeOut
Javis Perez
wow, that works in the demo! let me check, maybe is my mistake
Javis Perez
I can help more if you show the actual code. Paste it to http://jsbin.com
galambalazs
Ok i modified the chain function, so i can call it like: $('#element').chain().animate({opts}).fadeOut({opts}).end(); and doesnt work, :( i'll paste it to jsbin.com thank you for your help!! i'll let you know the link! :D
Javis Perez
Here's the link: http://jsbin.com/ecigo3/2/edit thank you!
Javis Perez
I didn't want to past my code into jsbin. A little bit about how you intergrate `Fx` to your framework would've helped. Never mind. I gave you the idea. The rest is up to you.
galambalazs
Ok, i updated the code, i added how i integrate the Fx with the rest of the code: http://jsbin.com/ecigo3/4/edit thank you for all your help, im trying to fix it myself.
Javis Perez
I can't see any problem here. Are you sure your animation code is bug free? I would give http://github.com/madrobby/emile a shot. It's 1,5 kB and does every kind of animation.
galambalazs
I'll check it out, thank you for all this help!!
Javis Perez