Jump to content
Search Community

Getting the progress of an individual tween within a timeline / scrollTrigger

mheavers test
Moderator Tag

Recommended Posts

Hi - I'm using GSAP's timeline, and I can successfully get the overall timeline's progress, but I want to get the progress of a specific tween within that timeline. How do I do this?

This is my code:

```

tl = gsap.timeline({
  // yes, we can add it to an entire timeline!
  scrollTrigger: {
    ...
    onUpdate: (timeline)=>{
      console.log(timeline.progress); //this works
    }),
  },
});
tl.from(".transform-title", { autoAlpha: 0, translateY: 20, onUpdate: (thisTween)=>{
  console.log(thisTween.progress) //this doesn't. are no parameters passed onUpdate? (thisTween undefined)
} }, "start");


```

Seems like the timeline as a whole is passing a reference to itself as a parameter to the onUpdate function so that I can get progress of the timeline as a whole, but the individual tweens are not.

Link to comment
Share on other sites

The problem with your code is that you're using an arrow function which locks scope to wherever it was defined! So if you just use a regular function, you'll be able to use this.progress() to get that tween's progress value. 

 

Also, keep in mind that a ScrollTrigger's onUpdate is different than a tween's onUpdate - the ScrollTrigger instance itself gets passed to the ScrollTrigger's onUpdate callback. A tween's onUpdate doesn't get any parameters by default, but "this" inside that [regular] function refers to the tween instance. 

 

tl = gsap.timeline({
  scrollTrigger: {
    ...
    onUpdate: trigger => console.log(trigger.progress)
  },
});
tl.from(".transform-title", { 
  autoAlpha: 0, 
  translateY: 20, 
  onUpdate: function() {
    console.log(this.progress());
  }
}, "start");

Does that clear things up?

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