Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
pizza0502

How to make scrollTo += works in React?

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi, I'm trying to use scrollTo in React. But I can't make the increment value to works with scrollTo...

 

Here is the function that not working:

handleClick = () => {
        let incrementNumber = 500
        TweenMax.to('content', 0.5, { scrollTo: { x:'+='+incrementNumber } }); 
    }

 

Here is the function that can works if I just use a fixed number, but like this I can make the scrollTo increase 500px every time I click it:

handleClick = () => {
        TweenMax.to('content', 0.5, { scrollTo: { x:500} }); 
    }
Link to comment
Share on other sites

Hi,

 

Actually this has nothing to do with React (how about that React is not the culprit, who would have suspected?? ;-)) but with the fact that the ScrollTo Plugin doesn't work with relative values like a regular tween using the CSS Plugin, that's why the first code you posted doesn't work (it actually doesn't work with or without React) and the second snippet does.

 

What you can do is access the scrollLeft property of the target element and add the increment value to that:

 

TweenMax.to(content, 0.5, {
  scrollTo: {
    x: (content.scrollLeft + incrementNumber)
  }
});

 

That should do the trick.

 

Let us know how that works.

 

Happy Tweening!!!

  • Like 4
Link to comment
Share on other sites

Sorry about any confusion there - I can make ScrollToPlugin accommodate relative values like that in the next release. In the mean time, feel free to use @Rodrigo's suggestion (though I think he meant "scrollLeft" rather than "scrollTop") :)

  • Like 4
Link to comment
Share on other sites

1 minute ago, GreenSock said:

though I think he meant "scrollLeft" rather than "scrollTop"

 

Indeed... the blunder has been corrected master!!! :D

  • Like 2
  • Haha 3
Link to comment
Share on other sites

Tested @Rodrigo answer and it works well! Thanks Rodrigo! ?

 

It's weird that I stumble upon multiple answers are told to use the solution like what I insert there...maybe they didn't test it out...

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.

×