Learning Posted February 27 Share Posted February 27 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 More sharing options...
GSAP Helper Posted February 27 Share Posted February 27 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: React (please read this article!) Next Svelte Sveltekit Vue Nuxt 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 More sharing options...
Learning Posted February 28 Author Share Posted February 28 Here's a pen on the issue, essentially, i'm trying to spread out the particles animating, but it seems they eventually sync up as the timeline repeats. https://codepen.io/kelvinzhao/embed/wvZwpzJ Link to comment Share on other sites More sharing options...
Solution GreenSock Posted February 28 Solution Share Posted February 28 It seems like it's doing exactly what it should according to the code, but I don't think you need a timeline for that - why not use a recursive function?: See the Pen gOyYKro?editors=0010 by GreenSock (@GreenSock) on CodePen Link to comment Share on other sites More sharing options...
Learning Posted March 7 Author Share Posted March 7 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 More sharing options...
GreenSock Posted March 7 Share Posted March 7 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 More sharing options...
Learning Posted March 8 Author Share Posted March 8 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now