Jump to content
Search Community

Multiple timelines - code check

CappMarvell test
Moderator Tag

Recommended Posts

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.

Link to comment
Share on other sites

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: 

 

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

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!

Link to comment
Share on other sites

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!

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