CappMarvell Posted November 21, 2023 Posted November 21, 2023 Hi, I want to have two sets of animations: Text that fades in and out on one A background image that slides from left to right on the other. This slide effect happens at the same time the text fades in and out Here's my code: const tl = gsap.timeline(); tl.to("#copy1", {duration: 1.0, autoAlpha: 1, onStart: function(){ const tl2 = gsap.timeline(); tl2.set("#slider", {autoAlpha: 1}); tl2.to("#slider", {duration: 9.0, right: 0}); return tl2; } }); tl.to("#copy1", {duration: 1.0, autoAlpha: 0, delay: 2.0}); tl.to("#copy2", {duration: 1.0, autoAlpha: 1}); tl.to("#copy2", {duration: 1.0, autoAlpha: 0, delay: 2.0}); tl.to("#copy3", {duration: 1.0, autoAlpha: 1}); return tl; The code works, but I'm wondering if it could be cleaner, and would like someone to check my code and suggest improvements, if any. Also... I'd like to set the duration of the slider image to the total time of the first timeline (tl). I tried: var total = tl.totalDuration(); tl2.to("#slider", {duration: total, right: 0}); ...but that had no effect. Any help would be appreciated. Thanks.
GSAP Helper Posted November 21, 2023 Posted November 21, 2023 It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or Stackblitz that demonstrates 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. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer. Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo: See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen. 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.
Rodrigo Posted November 21, 2023 Posted November 21, 2023 Hi, Code looks good to me. 11 hours ago, CappMarvell said: I'd like to set the duration of the slider image to the total time of the first timeline (tl). Regarding this, since the onStart callback is on the first tween of the timeline, you can add an eventCallback after the GSAP Timeline has been populated: cosnt tl = gsap.timeline(); tl.to(...); tl.to(...); tl.to(...); tl.eventCallback("onStart", function() { const tl2 = gsap.timeline(); tl2.to(element, { duration: tl.duration() }); }); If you keep having issues, please include a minimal demo. Happy Tweening!
CappMarvell Posted November 22, 2023 Author Posted November 22, 2023 Hi, That worked perfectly! I'm still unsure why totalDuration didn't work where duration did. I'll look over the docs again regarding that. And the code review is appreciated. Thanks.
Rodrigo Posted November 22, 2023 Posted November 22, 2023 Hi, Honestly I couldn't tell you. By the time the first tween starts in the timeline, the timeline should be fully rendered and should report the duration properly, like this: const a = { foo: 0 }; const tl = gsap.timeline(); tl.to(a, { foo: 10, duration: 1, onStart: function() { console.log(tl.totalDuration(), "-", tl.duration()); // -> 4 - 4 } }); tl.to(a, { foo: 20, duration: 1, }); tl.to(a, { foo: 30, duration: 1, }); tl.to(a, { foo: 40, duration: 1, }); But I'm glad that now is working as you expect. Happy Tweening!
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