Jump to content
Search Community

Opacity animations with ScrollTrigger not finishing when scrolling back up

mquint
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Posted (edited)

Hello!  I have fairly simple animations in a timeline with scroll trigger:

 

  gsap
    .timeline({
      scrollTrigger: {
        trigger: document.getElementById("scene-1"),
        scrub: 0.5,
        start: "top top",
        end: "bottom bottom"
      }
    })
    .addLabel("Scene 1")
    .to(".scene-1-1", { opacity: 0, y: -80, duration: 1 }, "+=2")
    .to(".bg-1-1", { opacity: 0, duration: 1 }, "-=1")
    .to(".scene-1-2", { opacity: 1, y: -40, duration: 1 })
    .to(".scene-1-2", { opacity: 0, y: -80, duration: 1 }, "+=2")
    .to(".bg-1-2", { opacity: 0, duration: 1 }, "<")
    .to(".scene-1-3", { opacity: 1, y: -40, duration: 1 })
    .to(".scene-1-3", { opacity: 0, y: -80, duration: 1 }, "+=2")

Text blocks fade in and out, and backgrounds fade out to reveal the next background.

 

Everything works fine when I'm scrolling down the page, but sometimes when I'm scrolling back up (going through the timeline in reverse) the text elements don't always return all the way to opacity 0. (I have the opacity defaulted to 0 in styles).

 

image.png.da09b061dbaed3fcc7dddbfc1e112a3d.png

 

Here's a demo version: https://codesandbox.io/s/interesting-volhard-yc6frk?file=/src/animations.js

Hoping there's something just I missed for scroll trigger settings? 

Edited by mquint
linked to less relevant file of code sandbox
  • Solution
Posted

Hi @mquint and welcome to the GSAP Forums!

 

The main issue is that you are working with React and you're not using GSAP Context:

https://gsap.com/docs/v3/gsap/methods/gsap.context()

 

Adding this to your animations.js file:

export default function animate() {
  gsap.registerPlugin(ScrollTrigger);

  const tl = gsap
    .timeline({
      scrollTrigger: {
        ...
      }
    })
    // ... instances here
    return tl;
}

And then making this change to your App.js file:

useLayoutEffect(() => {
  const ctx = gsap.context(() => {
    timeLine.current = animate();
  });
  return () => ctx.revert();
}, []);

Seems to work as you expect.

 

Finally I strongly recommend you to check the resources in this page:

https://gsap.com/resources/React

 

Hopefully this helps.

Happy Tweening!

 

 

Posted

Thank you, that fixed it! It's been a while since I looked at the react docs, context must be newer! 

  • Like 1
Posted
16 hours ago, mquint said:

context must be newer! 

Well... not really, it was released in 3.11 over a year ago:

https://gsap.com/blog/3-11/

 

Happy Tweening!

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