Jump to content
Search Community

Scroll Trigger working in dev mode but not build

Guest
Moderator Tag

Recommended Posts

I have a simple scrollTrigger animation in React. It works as expect in development mode but when I switch it too build the scrollTrigger animations start before youve scroll to the trigger. 

I have seen similar questions about this with the cause being something to do with chromes resizing and the advice was to use ScrollTrigger.refresh() im not sure where to use that as all the examples have been HTMl and not React.

I would like to be shown a solution that allows the scrollTrigger animations to work in production mode as well as development mode.

 

I have included all relevant plugins as shown:

import  * as React from 'react';
import { useEffect } from 'react';
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
 
gsap.registerPlugin(ScrollTrigger);

 

Heres the current code for the animation 

 
    //Animation
    useEffect(() => {
        ScrollTrigger.refresh();
 
          gsap.from('#aboutSection', {duration: 1, y: '10%', opacity: '0', ease: 'sine', delay: '0.5', scrollTrigger: {
            trigger: "#aboutSection",
        }});
    });
Link to comment
Share on other sites

I bet the problem is that 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?

 

So you can either turn off strict mode in React or you can add some conditional logic to your useEffect() call so that it only runs ONCE. Sorta like:

const didAnimate = useRef(false);

useEffect(() => {
  // if we already ran this once, skip!
  if (didAnimate.current) { return; }
  // otherwise, record that we're running it now and continue...
  didAnimate.current = true;
  gsap.from(el, {x: 100});
}, []);

 

Or you could just use .fromTo() tweens so that you define both the start and end values.

 

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

 

We're actually working on some exciting features in the next release that'll make this even easier to work around. 

Link to comment
Share on other sites

Im afraid I have managed to get a solution from this.

It seems all the scroll related animations trigger the instant the page loads the more I look.

 

I updated the component to include this:

 

    const didAnimate = useRef(false);
 
    //Animation
    useEffect(() => {
          // if we already ran this once, skip!
            if (didAnimate.current) { return; }
            // otherwise, record that we're running it now and continue...
            didAnimate.current = true;
 
          gsap.from('#aboutSection', {duration: 1, y: '10%', opacity: '0', ease: 'sine', delay: '0.5', scrollTrigger: {
            trigger: "#aboutSection",
        }});
    });
Edited by ELG0BLIN0
Added more detail to problem
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...