Jump to content
Search Community

Is there any way to run onUpdate function, during scrub?

anir test
Moderator Tag

Recommended Posts

const animation = gsap.to(node, {
      x: width,
      scrollTrigger: {
        trigger: '#our_clients_section',
        start: 'center bottom',
        end: 'bottom top',
        scrub: 3,
        onUpdate: (self) => {
          distanceTrainCrossed = parseInt(self.animation?._targets[0]._gsap.x)
        }
      }
    })

I need the distanceTrainCrossed sync with train object latest position, but as there is scrub enabled of 3s. During the scrub, the onUpdate function not firing. so i can't get the latest position.

Link to comment
Share on other sites

Hi @anir welcome to the forum!

 

Please provide a minimal demo when posting a question, that way we can dive directly in to your code instead of having to create a demo our selfs first. This saves time for everyone and gets you more chance of getting an answer on this forum! 

 

What is it you want to do exactly? You should never use properties with an underscore (_) these are only for internal use and could change in the future. Do you just want to get the value of the property you're animating? Check out `gsap.getProperty()` (https://gsap.com/docs/v3/GSAP/gsap.getProperty()/)

 

Hope it helps and happy tweening! 

 

See the Pen KKJmoww?editors=0010 by mvaneijgen (@mvaneijgen) on CodePen

  • Like 1
Link to comment
Share on other sites

Just to be clear, the ScrollTrigger's onUpdate fires every time the scroll position updates whereas the tween's onUpdate fires every time the playhead moves on the tween. So it sounds like you just need to move your onUpdate from the ScrollTrigger to the tween: 

 

const animation = gsap.to(node, {
      x: width,
      onUpdate() {
          distanceTrainCrossed = parseInt(gsap.getProperty(node, "x"))
      },
      scrollTrigger: {
        trigger: '#our_clients_section',
        start: 'center bottom',
        end: 'bottom top',
        scrub: 3
      }
    })

Does that help?

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