Jump to content
Search Community

GSAP modifier is not working as expected

tmusharraf test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

what_i_build_so_far.png.61b02f145fca9c4d070a994ba65f7b75.png

HERE is the CODE: https://stackblitz.com/edit/gsap-react-basic-f48716-ontwbj?file=src/App.js

I want to create the carousel. Boxes move from left to right and when the left: 100% it should start from the left: -40%

 

Here is my code:

 

CSS:

.moving_box{
  position: absolute;
  left: -50%;
}

React:

useLayoutEffect(() => {
    const ctx = gsap.context((self) => {
      var last_box_x_value = windowSize.innerWidth <= 424 ? "281%" : "200%";
      gsap.set(".moving_box", {
        y: (i) => i == 2 ? last_box_x_value : "60px",
        left: (i) => i == 0 ? "0" : i == 1 ? "60%" : i == 2 ? "40%" : ""
      });
  
      HowToSectionT1.current = gsap.timeline({
        defaults: {
          repeat: -1,
          ease: "Linear.easeNone",
          align: "start"
        }
      })
      .to(".box_1, .box_2, .box_3",
      {
        left: "+=100%",
        duration: 10,
        modifiers: {
          left: (x) => {
           
           // return x > 100 ? "-40%" : x + "%"; <- Problem is here
            return x;
          }
        }
      }
      )
    }, HowToSectionRefContainer); // <- Scope!
    return () => ctx.revert(); // <- Cleanup!
  }, []);

 

 

The Problem:

 

The problem is when I try the this code the all the boxes move to right but not follow the modifier (when left is 100% go back to -40%). thanks

 
 

 

 

 

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

37 minutes ago, GSAP Helper said:

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

 

If you're using something like React/Next/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

 

Here is the code: https://stackblitz.com/edit/gsap-react-basic-f48716-ontwbj?file=src/style.css

Link to comment
Share on other sites

There is a lot happening in your code and I can't make heads or tails from it. That said I'm not a React dev.

 

I do see that you animate the property left: which isn't that performant it is better to animate x: or in your case xPercent: that way you can just feed it a number instead of having to calculate percentages with each other. I would try that or provide a minimal demo with just some colored divs and the minimal amount of code to demonstrate the issue, this would increase your chance in getting a better answer. 

Link to comment
Share on other sites

1 hour ago, mvaneijgen said:

There is a lot happening in your code and I can't make heads or tails from it. That said I'm not a React dev.

 

I do see that you animate the property left: which isn't that performant it is better to animate x: or in your case xPercent: that way you can just feed it a number instead of having to calculate percentages with each other. I would try that or provide a minimal demo with just some colored divs and the minimal amount of code to demonstrate the issue, this would increase your chance in getting a better answer. 

Thanks for the reply, I try to create the minimal demo here: https://stackblitz.com/edit/gsap-react-basic-f48716-6tfu2d?file=src/App.js

 

I want to a similar `Carousel Wrap` you'll see here at the end of the article:

 

 

Now as you can see in demo there are two main problems:

 

1. When boxes reach 100% its doesn't stop and reappear from the left side, Instead they appear when first box reaches 100%,

2. When it reaches 0 you can see even I applied the CSS property of left: -50% to class moving_box

 

Possible Solutions:

I can imagine the solution but unable to achieve it.

 

1. By using the modifiers, we can reset the left to -50% whenever box reaches 100% so It will not pop-up suddenly instead create smooth animation? 

2. we can use the 6 boxes instead of three so it will mimic the animation like: https://greensock.com/modifiersplugin/

 

 

Thanks

Link to comment
Share on other sites

  • Solution

Hi,

 

I think you are overcomplicating this a bit. On top of that creating a single timeline for all the animations might not be the best way to tackle this.

 

I think is a better approach to create a single tween for each element, set a different progress for each one and loop those instances. Here is a simple demo:

https://stackblitz.com/edit/gsap-react-basic-f48716-9bv1q6?file=src%2Fstyle.css,src%2FApp.js

 

I also made a few changes in the CSS since I'm using transforms instead of top left, which will give you better performance.

 

Hopefully this helps.

 

Happy Tweening!

  • Like 2
Link to comment
Share on other sites

14 hours ago, Rodrigo said:

Hi,

 

I think you are overcomplicating this a bit. On top of that creating a single timeline for all the animations might not be the best way to tackle this.

 

I think is a better approach to create a single tween for each element, set a different progress for each one and loop those instances. Here is a simple demo:

https://stackblitz.com/edit/gsap-react-basic-f48716-9bv1q6?file=src%2Fstyle.css,src%2FApp.js

 

I also made a few changes in the CSS since I'm using transforms instead of top left, which will give you better performance.

 

Hopefully this helps.

 

Happy Tweening!

Thanks, How to stop them onMouseEnter?

Link to comment
Share on other sites

Hi I'm on my phone now so I can't give you the code for that. But at the top of my head I'd create an array for the boxes tweens and loop through them pausing and playing according to the specific event.

 

Another option is to add them to a timeline and play/pause that timeline.

 

Those are pretty simple and straightforward solutions that hopefully you're able to implement.

 

Happy Tweening!

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