Jump to content
Search Community

GSAP ScrollTrigger not working on initial load but works after scrolling through it

Owen Tam test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

I am trying to change the background color of body dynamically using scroll trigger on each section. However, it appears that after refreshing the site (codepen), what happens is that scrolling to the second part (the one with the id of #Codequest) does not trigger the background color change. If I try to scroll back up and down, the mentioned change suddenly works. Is there any reason as to why this is happening and how to fix it?
Thanks!

Edited by Owen Tam
Outdated link
Link to comment
Share on other sites

Hi @Owen Tam welcome to the forum!

 

GSAP is build for performance, so if you tell it to animate to something, it will setup all kinds of things under the hood, so that it only has to animate and not figure out what to animate when you tell it to. Thus what is happening on page load it will calculate everything it needs to animate and because you tell it to animate the same thing twice it will cause a conflict, you can solve this conflict by telling it not to render everything on page load with immediateRender: false

 

The toggleActions  you set where also conflicting, because you tell the first ScrollTrigger to reverse on leave, but at the same time the other ScrollTrigger was playing, so the toggleActions: "play none none reverse", is what you want. 

 

As a side note: background is a short hand for all kinds of background properties (see https://developer.mozilla.org/en-US/docs/Web/CSS/background). If you know you're only going to animate the background-color it is better to just animate that property eg backgroundColor in GSAP 

 

Hope it helps and happy tweening! 

 

See the Pen ZEmNLKo?editors=1010 by mvaneijgen (@mvaneijgen) on CodePen

  • Like 1
Link to comment
Share on other sites

Thanks a lot! The problem above has been fixed now. However, the reason that I put toggleActions: "play reverse play reverse" initially was that I wanted to revert back to the original background color (black in this case) after leaving the last project container. Is there any way to also allow that or is the only way using ScrollTrigger at the last container (manually?) to revert the body background color?

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