Jump to content
Search Community

A question for ScrollTrigger - onLeave, onEnter, etc

Nelou test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hello everyone,

I've been looking around the docs, but I don't seem to find the answer.

 

We do have a way to trigger js with "onLeave" "onEnter" etc, but is there a parameter in scrolltrigger that would let us use js at 50% of the scrolltrigger ?

let tl = gsap.timeline({
    scrollTrigger: {
        trigger: trigger,
        start: 'top top',
        end: 'bottom bottom',
        onEnter: () => {},
      	???: () => {
  			//here i'd need to use js at, let's say, halfway through the ST
		}
        onLeave: () => {}
    }
});

or do I need to create a specific ST for that specific value ?

 

Thanks a lot for your help,

 

Best regards,

 

Nelou

Link to comment
Share on other sites

  • Solution

Hi,

 

Actually there is no callback for onMidPoint or something like that. If you want to do something mid-way through the ScrollTrigger instance there are a few options:

  1. Use the onUpdate callback and check the progress of the ScrollTrigger instance:
    ScrollTrigger.create({
      start: "top top",
      end: "+=100%",
      onUpdate: (self) => {
        if (self.progress === 0.5) {
          // Do something here
        }
      },
    });

    But that could be a little overkill, since you'd be running that particular logic on every update so it could become expensive.

  2. Use a timeline (like you're doing right now) and add a call method midway through the timeline using the position parameter:
     

    let tl = gsap.timeline({
      scrollTrigger: {
        trigger: trigger,
        start: 'top top',
        end: 'bottom bottom',
        onEnter: () => {},
        onLeave: () => {},
      }
    });
    tl
      .to(/* animation */)
      .to(/* animation */)
      .to(/* animation */)
      // At the end of the timeline, after adding all the animations
      .call(() => {
        /* This will executed midway through the timeline */
      }, null, (tl.duration() / 2));

There might other ways but they could be more complicated than this. I'd go with the timeline approach.

 

Hopefully this helps.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

15 hours ago, Rodrigo said:

Hi,

 

Actually there is no callback for onMidPoint or something like that. If you want to do something mid-way through the ScrollTrigger instance there are a few options:

  1. Use the onUpdate callback and check the progress of the ScrollTrigger instance:
    ScrollTrigger.create({
      start: "top top",
      end: "+=100%",
      onUpdate: (self) => {
        if (self.progress === 0.5) {
          // Do something here
        }
      },
    });

    But that could be a little overkill, since you'd be running that particular logic on every update so it could become expensive.

  2. Use a timeline (like you're doing right now) and add a call method midway through the timeline using the position parameter:
     

    let tl = gsap.timeline({
      scrollTrigger: {
        trigger: trigger,
        start: 'top top',
        end: 'bottom bottom',
        onEnter: () => {},
        onLeave: () => {},
      }
    });
    tl
      .to(/* animation */)
      .to(/* animation */)
      .to(/* animation */)
      // At the end of the timeline, after adding all the animations
      .call(() => {
        /* This will executed midway through the timeline */
      }, null, (tl.duration() / 2));

There might other ways but they could be more complicated than this. I'd go with the timeline approach.

 

Hopefully this helps.

Happy Tweening!

Hello !

 

2. is the solution I'm after.

 

Craig, Jack, Rodrigo, thanks for your time, you guys are awesome !

 

Have a good day.

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