Jump to content
Search Community

How to skip part of a timeline if element does not exist ?

aledoro test
Moderator Tag

Recommended Posts

Hello everyone !
I'm new to GSAP and just started using it in a React project.

I'm mapping an array of elements and apply timelines to each one so they play in sequence.

Basically what i want is to skip the last .fromTo(line[index], {width: 0}, {width: "100%}) when (index == cards.lenght -1) since that last element does not exists and i get a "GSAP target not found error"

I know i could use an if/else statement and copy the timeline minus this line, it works, but this is not a very elegant solution and forces to apply changes in both if i were to tweak the animation
Is there a clean way to do this ?

Thanks !

Here is my code : 
 

cards.map((card, index) => {
tl.current = gsap
.timeline()
.delay(index + 1.5)
.fromTo(card, { opacity: 0 }, { opacity: 1 })
.fromTo(dots[index], { opacity: 0 }, { opacity: 1 }, "<")
.fromTo(line[index], { width: 0 }, { width: "100%" })
.timeScale(1.25);
});
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 CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). 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

 

If you're using something like React/Next/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

 

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

Have you ever used Stagger? This you can just give an array of all your elements you want to animate and it will calculate the logic for you, no need to map over specific items, just give it `document.querySelectorAll('.lines')` and add the `stagger:{}` object with your parameters. 

 

But this is all pseudo code, a minimal demo would really help. 

  • Like 1
Link to comment
Share on other sites

I have found a solution, i though i had to chain everything all at once on the timeline
But this actually works :
 

cards.map((card, index) => {
tl.current = gsap
.timeline()
.delay(index + 1.5)
.fromTo(card, { opacity: 0 }, { opacity: 1 })
.fromTo(dots[index], { opacity: 0 }, { opacity: 1 }, "<");
index != cards.length - 1 &&
tl.current.fromTo(line[index], { width: 0 }, { width: "100%" });
tl.current.timeScale(1.25);
});
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...