Jump to content
Search Community

GSAP ScrollTrigger don't work as espected in my timeline

Frontend.dev test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hello guys from GSAP community!
  You have created a cool tool, congratulations to you. And now, I'm trying to recreate the text animation in the second section from Apple's website: https://www.apple.com/airpods-pro/
The problem is that ScrollTrigger doesn't behave quite as I expected and instead of the Fade-In-Out effect on individual sentences like on the reference, I have the transparency of the entire text changing as soon as it enters the viewport. Can you please tell me what I'm doing wrong? Maybe someone has already tried to recreate this animation?

Screenshot 2024-01-09 at 21.07.51.png

See the Pen MWxeqdB by sokolenkov (@sokolenkov) on CodePen

Link to comment
Share on other sites

  • Solution

Hi @Frontend.dev and welcome to the GSAP Forums!

 

The problem is that you are targeting all the elements with this selector:

tl
  .to(".list-item", { duration: 0.5, opacity: 1 })
  .to(".list-item", { duration: 0.5, opacity: 0 }, 0.5);

So they all animate at the same time. Also a couple of notes, the default duration of a GSAP Tween is 0.5 seconds, so there's no need to set the duration in the config.

You can achieve this in two ways, looping through the elements:

const tl = gsap.timeline({
  scrollTrigger: {
    trigger: ".section",
    start: "top center",
    end: "bottom center",
    markers: true,
    scrub: true
  }
});

const items = gsap.utils.toArray(".list-item");
items.forEach((item, i) => {
  tl.to(item, { opacity: 1 })
  if (items[i - 1]) {
    tl.to(items[i - 1], { opacity: 0.3 }, "<")
  }
});

Or using staggers and a simple conditional block for the opacity of the last element:

const tl = gsap.timeline({
  scrollTrigger: {
    toggleActions: "play reverse play reverse",
    trigger: ".section",
    start: "top top",
    end: "bottom bottom",
    markers: true,
    scrub: true
  }
});
const items = gsap.utils.toArray(".list-item");
const amount = items.length;
tl
  .to(items, { stagger: 0.5, opacity: 1 })
  .to(items, {
    stagger: 0.5,
    opacity: (i) => (i < amount - 1) ? 0.3 : 1,
  }, 0.5);

Here is a fork of your demo using the stagger approach:

See the Pen rNRLQVx by GreenSock (@GreenSock) on CodePen

 

Finally thanks for the simple and minimal demo, we love those around here! 🥳

 

Hopefully this helps.

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