Jump to content
Search Community

How to make scrollTo += works in React?

pizza0502 test
Moderator Tag

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

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