Jump to content
Search Community

Using function-based values with delay

Dipscom test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Use the mighty for-loop. It's faster and less code.

 

var divs = document.querySelectorAll("div");

for (var i = 0; i < divs.length; i++) {
  TweenMax.to(divs[i], 1, {
   xPercent: Math.random() * 200,
   ease: Power1.easeInOut,
   delay: i * 2,
   repeat: -1,
   yoyo: true
  });
}

 

  • Like 1
Link to comment
Share on other sites

I am using that, yes. Just praying for some godly action here.

 

But, now I have to transform a HTMLCollection into a real array in order to manipulate it to my hearts content.

 

At least it warms my belly to know I have taken the path mighty demigod @OSUblake has shown. Take pity on this ant oh mighties!

Link to comment
Share on other sites

Node list to array.

function selectAll(selector, context) {
  var nodeList = (context || document).querySelectorAll(selector);
  return Array.prototype.slice.call(nodeList);
}

 

More code, but faster.

function selectAll(selector, context) {
  var nodeList = (context || document).querySelectorAll(selector);
  var i = nodeList.length;
  var elements = new Array(i);
  while (i--) { elements[i] = nodeList[i]; }
  return elements;
}

 

 

 

  • Like 3
Link to comment
Share on other sites

Great question, Pedro. 

 

The he issue here is that even though you can have a tween that has multiple targets that have have multiple properties being animated to unique values it is all being controlled by ONE tween that you can play, pause, etc. 

 

delay changes the start time of a tween and you can't have a single tween with multiple start times. Make sense?

 

Again, best to heed the wisdom of mighty Blake. 

  • Like 4
Link to comment
Share on other sites

You could just switch to a staggerTo() so that you can use the cycle feature to do that:

 

TweenMax.staggerTo("div", 1, {
        xPercent:function() {
            return Math.random() * 200
        },
        repeat: -1,
        yoyo: true,
        ease: Power1.easeInOut,
        cycle:{delay: function(i) {
            return i * 2;
        }}
});

 

Carl is exactly right about why a function-based delay on a regular tween wouldn't work. The staggerTo() spits back an array of tweens - that's why it'll work there. And you don't have to define a stagger if you don't want to. 0 is the default. 

  • Like 3
Link to comment
Share on other sites

I haven't quite grasped what Carl has said but I am confident it will eventually sink in.

 

@PointC more coffee to you, sir. Whatever day you may be having, coffee is the solution.

 

Thanks Jack. I shall cheat like you are suggesting just for the sake of doing so (as mentioned before, I had it working with the for loop).

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...