Jump to content
Search Community

ScrollTigger animation firing on page load and not on reaching the trigger element

RGR test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

  • import React, { useState, useEffect, useRef } from "react";
  • import { gsap } from "gsap";
  • import { ScrollTrigger } from 'gsap/ScrollTrigger';
  • import LandingHeader from "../../components/LandingPageComp/LandingHeader/LandingHeader.jsx";
  • import * as S from "./LandingPage.style.js";
  •  
  • const LandingPage = () => {
  • const mainRef = useRef();
  • const headerRef = useRef();
  • const greySec = useRef();
  •  
  • const ANIMATION_TIME = 1.5;
  • const ANIMATION_TYPE = "ease-in-out";
  • const PAGE_DELAY = 0;
  •  
  • const SETIMEOUT_DELAY = 3000;
  •  
  • useEffect(() => {
  • mainRef.current.focus();
  • mainRef.current.style.height = `${window.innerHeight}px`;
  • gsap.registerPlugin(ScrollTrigger)
  • ScrollTrigger.defaults({
  • toggleActions: "play none restart reset",
  • start: "top center",
  • end: "bottom center",
  • });
  •  
  • setTimeout(() => {
  •  
  • gsap.to(headerRef.current, {
  • scrollTrigger: {
  • trigger: greySec.current,
  • markers: true,
  • start: "top center"
  • },
  • background: '#fff',
  • color: '#000',
  • duration: 0,
  • });
  •  
  • }, SETIMEOUT_DELAY);
  • }, []);
  •  
  • return (
  • <React.Fragment>
  • <div ref={mainRef} tabIndex={1}>
  • <LandingHeader headerRef={headerRef} />
  • <S.LandingGreySec ref={greySec}>
  • </S.LandingGreySec>
  • </div>
  • </React.Fragment>
  • );
  • };
  •  
  • export default LandingPage;
Link to comment
Share on other sites

  • RGR changed the title to ScrollTigger animation firing on page load and not on reaching the trigger element

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/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

  • Thanks 1
Link to comment
Share on other sites

  • Solution

Hi @RGR and welcome to the GreenSock forums!

 

Mostly this is a CSS issue. The mainRef element that you're using as scroller for scroll trigger has a height of 100%, but 100% of what exactly? That element doesn't have any particular ancestor with a given height, they all have the default value (auto), so that grows as large as it's content. Because of that your ScrollTrigger instance doesn't work.

 

If you change your styles to this:

<div className="test" style={{ overflow: "auto", height: "100vh", width: "100%" }} ref={mainRef}>

And add these styles in the index.css file:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  overflow: hidden;
}

Everything seems to work as expected:
https://codesandbox.io/s/frosty-cookies-bemgvz?file=/src/App.js

 

Happy Tweening!!!

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