Javascript - effects queue (chain)

im creating an animation environment for my work, and im - in the part of the queue or fragment of the chain, in fact 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 great! but fadeIn and the animation are running at the same time, but what I want to do is something like:

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

so that it performs the animation first and then 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 something like that, but I don’t know how to do this for the array or even call the first method, because I try to get all the requests to the array and just call it every time if the effects are done.

im not using jQuery since i said i need to create a personalized structure.

Any idea pleeeasse ??! Thanks you

+3
1

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
    };
  }
};

Fx.chain(el).animate("style", 300).fadeIn(600).animate("style2", 900).end();
+4

Source: https://habr.com/ru/post/1772382/


All Articles