Jump to content
Search Community

Why are my timelines playing in order and not synchronously

floopix test
Moderator Tag

Go to solution Solved by mvaneijgen,

Recommended Posts

Hello,

I am new to gsap and React so bear with me. I currently have this which maps different timelines to different image slides for an image slider and I want the new slide to fade in while the old slide fades out at the same time. For some reason the timelines play one after the other so a fade out then a fade in. I am stuck because I am not sure what is causing them not to play in sync. Thank you for your time.

 


useLayoutEffect(() => {
    ctx.current = gsap.context((self) => {
      const slide = gsap.utils.toArray('.slide');
      const links = gsap.utils.toArray('.information-holder')
      const text = links.map((link) => link.querySelector('.header'));
      const button = links.map((link) => link.querySelector('.link'));
      anims.current = slide.map((slide,i) => {
        let tl = gsap.timeline({reversed: i === 0 ? false : true, duration: 1});
        tl.to(
          slide,
          {
            opacity: 1,
            display: 'block',
          },
          0,
        );
        tl.to(
          text[i],
          {
            opacity: 1,
            yPercent: 100,
            display: 'block',
          },
          0,
        );
        tl.to(
          button[i],
          {
            opacity: 1,
            yPercent: 100,
            display: 'block',
          },
          0,
        );
        return tl;
      });
    }, slidesRef);
    return () => ctx.current.revert();
  }, []);

  const clickFunction = (index) => {
    changeButton(index);
    anims.current[currentIndex.current].reversed(true);
    anims.current[index].reversed(false);
    currentIndex.current = index;
  };

Here is a demo:

https://stackblitz.com/edit/gsap-react-basic-f48716-thcer6?file=src%2FApp.js
 

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 @floopix welcome to the forum! 

 

As stated it is hard to debug without the code in some sort of environment where we can manipulate it, a minimal demo is great for that. Our preferred platform is Codepen.io, but if you must use React you can also use our Stackblitz templates.

 

The only thing I can see in your code is tweening the property  display: 'block', this you can't tween, because there are only ever two states, probably in your case 'none' and 'block', there is no in between there. If you're looking to hide the elements on page load, check out our FOUC blogs post 

 

Link to comment
Share on other sites

Thank you all for the speedy replies and suggestions, here is a basic demo:

https://stackblitz.com/edit/gsap-react-basic-f48716-thcer6?file=src%2FApp.js

Each button is related to a slide and my desired effect is for the next slide to fade in while the old slide fades out, but as you can see in the demo, first the new slide fades in then after that timeline has ended the old slide fades out. I am thinking it has something to do with setting the reversed of the two timelines to the opposite of one another but I am not completely sure.

Link to comment
Share on other sites

  • Solution

I've removed duration: 1 from the timeline, to my knowledge you can't set it that way, you can set a duration in the defaults: {} object, and that will set the duration for each tween on the timeline.

 

I have no idea why this would make it that timelines play in sequence, but it does fix the issue. 

 

I've also removed the position parameter "0", because that would do nothing, this you can use to set tweens relative to the other tween on the timeline, but in your case each timeline is separate.

 

Hope it helps and happy tweening! 

 

https://stackblitz.com/edit/gsap-react-basic-f48716-fz8kew?file=src%2FApp.js,src%2Fstyle.css

  • Like 2
  • Thanks 1
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...