Jump to content
Search Community

Particle timeline issue

Learning test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hi guys,

I have a set of divs that I want to simply loop infinitely moving from bottom of the screen to the top and stagger them.
This is the code I'm using, I have a problem because the timelines of each div is of a different length, thus when it repeats, it eventually chases up and end up with all divs moving at the same time. How can I fix this?

```

const promises = document.querySelectorAll(".promise");
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
 
const reset = (promise) => {
const width = windowWidth * gsap.utils.random(low, high);
gsap.set(promise, {
opacity: 1,
width: width,
y: () => -width,
x: () => gsap.utils.random(-width / 2, windowWidth - width / 2),
});
};
 
promises.forEach((promise, i) => {
reset(promise);
const tl = gsap.timeline(
{
repeat: -1,
ease: "none",
onRepeat: reset,
onRepeatParams: [promise],
},
0,
);
 
tl.from(
promise,
{
duration: 1,
y: windowHeight + promise.getBoundingClientRect().height,
},
i,
);
});

```

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hey Jack,

Thanks for your reply. It does make sense to make it a recursive function.
What I'm a little confused about is why onComplete we are adding a max delay?
I tested and we do have to add that, but I can't seem to logically understand it.
After the initial delay, wouldn't each recursive call be already delayed, so we don't have to add a delay anymore?

Link to comment
Share on other sites

2 hours ago, Learning said:

What I'm a little confused about is why onComplete we are adding a max delay?

It's a logic thing to maintain the spacing time-wise. For example, let's say the last one runs after 10 seconds (first delay). If you omit the delay on the next call, it'll play immediately after that instead of waiting another 10 seconds. A "delay" is only for the first iteration. Perhaps you wanted to use a repeatDelay?

Link to comment
Share on other sites

I think I got it.
My main issue was that if my item 10 is already delayed 10 seconds, even if it plays immediately it is already delayed. But I realised that's why all my items are slowly syncing up. They still should all be max delayed so it continues the spacing.
Thank you! Always nice to learn!

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...