Jump to content
Search Community

Issue with ScrollTrigger fade-in/out, overlay shouldn't be visible

MichaelT2023 test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hey guys,

 

maybe I'm completely blind now after trying to get this to work for hours, but maybe you can help me out? I think the solution is very simple, but I just don't get it.


So the overlay element shouldn't be visible, until the top of the first panel hits the center of the viewport.
Then the overlay should fade in, with opacity: 1, when the center of the first panel hits the center of the viewport.
When the center of the last panel hits the center of the viewport, the overlay should start to fade out, being completely invisble
when the bottom of the last panel hits the center of the viewport.

But the overlay is visible, when I start the page, doesn't matter, if I set the opacity to 0 with CSS or GSAP. Also I had issues when I move the overlay to the x/y-center of the viewport (commented out in css). But that seems to work now.

Maybe you can give me a gentle push in the right direction. :-) And if you have any suggestions regarding the overall GSAP code, ideas are very welcome.

Thank you
Michael


 

See the Pen xxaNrBO by MichaelT2023 (@MichaelT2023) on CodePen

Edited by MichaelT2023
Added CodePen, removed HTML, JS, CSS source code
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

  • Solution

Ah, that's because you're animating the SAME property (opacity) of the SAME element with multiple ScrollTriggers and you're using relative tweens like .from() and .to() which depend on the CURRENT state for either the start or end. And by default, ScrollTrigger-related tweens render immediately. 

 

So let's walk through it...

 

The initial opacity is 1 when the page loads, and you first create a .from(... {opacity: 0}) tween so says "use the CURRENT value (1) as the destination...and IMMEDIATELY render the provided opacity (0)". Great. Now the opacity is 0. But then you create a .to(... {opacity: 0}) tween (here's the problem), and since it renders immediately it says "grab the CURRENT value (0) and use that as the starting value, and then animate to the value this tween provides (0)". DOH! So you're animating from 0 to 0 (no change). 

 

There are various solutions, but I think the simplest would be to set opacity 0 in your CSS to avoid the initial flash (FOUC), and then use a .to(...{opacity: 1}) and then for the 2nd tween, do to(...{opacity: 0}) but set immediateRender: false to force it to wait until the scroll hits that position to render. 

 

See the Pen JjaqBKP?editors=1010 by GreenSock (@GreenSock) on CodePen

 

Does that clear it up? 

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