Jump to content
Search Community

During scrolling down, the animation doesn't play. Next.js + Gsap

Soorqs test
Moderator Tag

Go to solution Solved by Cassie,

Recommended Posts

При прокрутке вниз анимация не воспроизводится, а при прокрутке вверх срабатывает. Gsap/Next.js

Когда анимация должна запуститься, она просто не появляется. Его можно увидеть только при прокрутке вверх.

https://mn3pzk-3000.csb.app/  это пример

 

'use client';
import gsap from 'gsap';
import React from 'react';
import { ScrollTrigger } from 'gsap/ScrollTrigger';

const AboutComponent: React.FC = () => {
    const parent = React.useRef<HTMLDivElement | null>(null);

    React.useEffect(() => {
        gsap.registerPlugin(ScrollTrigger);
        const tl = gsap.timeline({
            scrollTrigger: {
                trigger: parent.current,
                markers: true,
                start: '50% 50%',
                end: '150% 50%',
                scrub: 2,
                pin: true,
            },
        });
        tl.to(
            '#center',
            {
                height: '100vh',
            },
            'a'
        )
            .to(
                '#top-el',
                {
                    top: '-50%',
                },
                'a'
            )
            .to(
                '#bottom',
                {
                    bottom: '-50%',
                },
                'a'
            )
            .to(
                '#top-title',
                {
                    top: '60%',
                },
                'a'
            )
            .to(
                '#bottom-title',
                {
                    bottom: '-30%',
                },
                'a'
            )
            .to(
                '#center-h1',
                {
                    top: '-30%',
                },
                'a'
            )
            .to('#content', {
                delay: -0.2,
                marginTop: '0%',
            });

        ScrollTrigger.refresh();
    }, []);

    return (
        <div
            ref={parent}
            className="relative w-full h-screen overflow-hidden bg-cyan-500"
            id="parent"
        >
            <div
                id="top-el"
                className="absolute top-0 w-full h-[50dvh] bg-black z-10 overflow-hidden"
            >
                <h1
                    id="top-title"
                    className="text-[22vw] absolute top-[50%] left-[50%] translate-x-[-50%] translate-y-[-50%] bottom-[50%]"
                >
                    GRAVITY
                </h1>
            </div>
            <div
                id="center"
                className="relative w-full h-screen overflow-hidden transition-all origin-center bg-white ease-cubic-bezier"
            >
                <div
                    className="mt-[100vh] flex flex-col justify-center items-center w-full h-screen text-black gap-[4vh]"
                    id="content"
                >
                    <h4 className="text-[1vw]" id="center-h1">
                        GRAVITY
                    </h4>
                    <h3 className="w-1/5 text-[3vw] text-center">
                        <i>Browse</i> the work that define a <i>movement</i> and
                        created a craft.
                    </h3>
                    <div className="flex items-center justify-center w-[7vw] h-[2vw] rounded-full bg-white text-black">
                        <h5>ENTER GALLERY</h5>
                    </div>
                    <h2 className="text-[20vw]">(17)</h2>
                </div>
            </div>
            <div
                id="bottom"
                className="absolute bottom-0 w-full h-[50vh] bg-black overflow-hidden"
            >
                <h1
                    id="bottom-title"
                    className="text-[220px] absolute top-0 left-[50%] translate-x-[-50%] translate-y-[-50%] bottom-[50%]"
                >
                    GRAVITY
                </h1>
            </div>
        </div>
    );
};

export default AboutComponent;

 

Link to comment
Share on other sites

When you're ready to move your demo to React - please pay attention to animation cleanup.

Proper animation cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

In GSAP 3.11, we introduced a new gsap.context() feature that helps make animation cleanup a breeze. All you need to do is wrap your code in a context call. All GSAP animations and ScrollTriggers created within the function get collected up in that context so that you can easily revert() ALL of them at once.

Here's the structure:

// typically it's best to useLayoutEffect() instead of useEffect() to have React render the initial state properly from the very start.
useLayoutEffect(() => {
  let ctx = gsap.context(() => {
    // all your GSAP animation code here
  });
  return () => ctx.revert(); // <- cleanup!
}, []);

This pattern follows React's best practices, and one of the React team members chimed in here if you'd like more background.

We strongly recommend reading the React information we've put together at https://gsap.com/resources/React/

Happy tweening!

  • Like 1
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...