RGR Posted September 21, 2022 Share Posted September 21, 2022 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 More sharing options...
GSAP Helper Posted September 21, 2022 Share Posted September 21, 2022 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. 1 Link to comment Share on other sites More sharing options...
RGR Posted September 21, 2022 Author Share Posted September 21, 2022 https://codesandbox.io/s/strange-tharp-e59od3?file=/src/App.js The background of header should change with the background color of the sections. Link to comment Share on other sites More sharing options...
Solution Rodrigo Posted September 21, 2022 Solution Share Posted September 21, 2022 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now