Jump to content
Search Community

Pin and Scrubbed component jumps down after animation ends

herougan test
Moderator Tag

Recommended Posts

https://stackblitz.com/edit/react-nehjjp?file=src%2FApp.js,src%2Fstyle.css

 

I want to create an animation where as you scroll, the image is pinned on the screen with images overlayed over that slowly reveal the image behind.

However, after the animation is done, there is extra pinSpacing at the bottom and so the images skip to the bottom.

 

I expect that when the animation ends, it should be at the end of the page, and there is no more to scroll down.

 

On another note, when refreshing, the animation does not appear at the start but slightly in front. Scrolling with the animation past the end then back resets it correctly.

I read this on the FAQ: "Why does my "scrub" animation jump on initial load? Or my non-scrub animation start playing? ..." but I don't understand the answer "Most likely the ScrollTrigger’s start value is before the starting scroll position. This usually happens when the start is something like "top bottom" (the default start value) and the element is at the very top of the page. If you don’t want this to happen simply adjust the start value to one that’s after a scroll position of 0."

Link to comment
Share on other sites

Hi @herougan and welcome to the GreenSock forums!

 

It seems that you are another victim of React's doble effect call on Strict Mode:

 

React 18 runs in "strict" mode locally by default which causes your useEffect() to get called TWICE! Very annoying. It has caused a lot of headaches for a lot of people outside the GSAP community too.

 

.from() tweens use the CURRENT value as the destination and it renders immediately the value you set in the tween, so when it's called the first time it'd work great but if you call it twice, it ends up animating from the from value (no animation). It's not a GSAP bug - it's a logic thing.

 

For example, let's say el.x is 0 and you do this: 

useEffect(() => {
  // what happens if this gets called twice?
  gsap.from(el, {x: 100})
}, []);

 

The first time makes el.x jump immediately to 100 and start animating backwards toward the current value which is 0 (so 100 --> 0). But the second time, it would jump to 100 (same) and animate back to the current value which is now 100 (100 --> 100)!  See the issue?

 

In GSAP 3.11, we introduced a new gsap.context() feature that solves all of this for you. All you need to do is wrap your code in a context call, and then return a cleanup function that reverts things: 

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

 

One of the React team members chimed in here if you'd like more background.

 

This seems to work in the way you intend:

useEffect(() => {
  const ctx = gsap.context(() => {
    gsap
      .timeline({
      scrollTrigger: {
        trigger: '.blinder__image--dark',
        start: 'top 0%',
        end: () => `+=${blinder_height}vh`,
        scrub: true,
        pin: '.blinder__cover--left',
        pinSpacing: false,
      },
    })
      .fromTo(
      '.blinder__cover--left',
      { opacity: 0.5 },
      {
        opacity: 0.1,
        xPercent: 50,
      }
    );

    gsap
      .timeline({
      scrollTrigger: {
        trigger: '.blinder__image--dark',
        start: 'top 0%',
        end: () => `+=${blinder_height}vh`,
        scrub: true,
        pin: '.blinder__cover--right',
        pinSpacing: false,
      },
    })
      .fromTo(
      '.blinder__cover--right',
      { opacity: 0.5 },
      {
        opacity: 0.1,
        xPercent: -50,
      }
    );

    gsap.timeline({
      scrollTrigger: {
        trigger: '.blinder__image--dark',
        start: 'top 0%',
        end: () => `+=${blinder_height}vh`,
        scrub: true,
        pin: true,
        markers: true,
      },
    });
  });
  return () => ctx.revert();
}, []);

I recommend you to check the resources in this page:

We also have a collection of starter templates using GSAP and React:

https://stackblitz.com/@GreenSockLearning/collections/gsap-react-starters

 

Hopefully this helps.

Happy Tweening!

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