Jump to content
Search Community

How do you work with Next.js ?

sarthak950 test
Moderator Tag

Recommended Posts

@OSUblake 

"use client";
import styles from "./page.module.css";
import { useRef, useState, useEffect } from "react";

import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import ScrollToPlugin from "gsap/ScrollToPlugin";
gsap.registerPlugin(ScrollTrigger);

import Character from "../components/Model/Character";

export default function aquatica() {
    const slide1 = useRef(null);
    const slide2 = useRef(null);
    const slide3 = useRef(null);
    const slide4 = useRef(null);
    const page = useRef(null)

    const [windowWidth, setWindowWidth] = useState();

    useEffect(() => {
        // Update window width
        const updateWindowWidth = () => {
            setWindowWidth(window.innerWidth);
        };
        window.addEventListener("resize", updateWindowWidth);
        updateWindowWidth(); // Initial width

        return () => {
            window.removeEventListener("resize", updateWindowWidth);
        };
    }, []);

    useEffect(() => {
        const slideArr = [slide1, slide2, slide3, slide4];

        const tl = gsap.timeline({
            scrollTrigger: {
                trigger: "page",
                start: "top 0%",
                end: `+=${windowWidth * 10}`,
                scrub: 1,
                pin: true,
                anticipatePin: 1,
            },
        });

        slideArr.forEach((slide) => {
            tl.to(slide.current, {
                x: -windowWidth * 2,
                duration: 1,
                ease: "none",
            });
        });
    }, [windowWidth]);

    return (
        <div ref={page} className={styles.page}>
            <div className={styles.heroSec}>
                <div className={styles.heroImg}></div>
                <div className={styles.heroContent}>
                    <div className={styles.heroNav}>
                        <img src="/icons/vorld.png" alt="vorld" />
                    </div>
                    <div className={styles.heroTitle}>
                        <h1>Welcome to aqutica</h1>
                        <p>
                            Embark on a voyage where the marvels<br />{" "}
                            of a boundless ocean planet unfold before you
                        </p>
                    </div>
                    <div className={styles.herofooter}>
                        <div className={styles.slider}>
                            <div className={styles.slideContent}>
                                <h1>EARN</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>AQUATICA</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1>SHOOT</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>PIONEER</h1>
                                <img src="./Aquatica/star.png" alt="star" />

                                <h1>EARN</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>AQUATICA</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1>SHOOT</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>PIONEER</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                            </div>
                            <div className={styles.slideContent}>
                                <h1>EARN</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>AQUATICA</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1>SHOOT</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>PIONEER</h1>
                                <img src="./Aquatica/star.png" alt="star" />

                                <h1>EARN</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>AQUATICA</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1>SHOOT</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                                <h1 className={styles.hollow}>PIONEER</h1>
                                <img src="./Aquatica/star.png" alt="star" />
                            </div>
                        </div>
                    </div>
                </div>

                <div className={styles.shadeCover}></div>
            </div>

            <div id="scrollbody">
                <section
                    ref={slide1}
                    className={`${styles.slide} ${styles.slide1}`}
                    id="slide_1"
                >
                    <h1 className={styles.centerHeading}>This is Cool</h1>
                </section>

                <section
                    ref={slide2}
                    className={`${styles.slide} ${styles.slide2}`}
                    id="slide_2"
                >
                    <h1 id="translate" className={styles.centerHeading}>
                        This is Cool
                    </h1>
                </section>

                <section
                    slide={slide3}
                    className={`${styles.slide} ${styles.slide3}`}
                    id="slide_3"
                >
                    <h1 className={styles.centerHeading}>This is Cool</h1>
                </section>

                <section
                    ref={slide4}
                    className={`${styles.slide} ${styles.slide4}`}
                    id="slide_4"
                >
                    <h1 className={styles.centerHeading}>This is Cool</h1>
                </section>
            </div>

            <div className={styles.temp}></div>
        </div>
    );
}

this is also not working can u guide me a little

Link to comment
Share on other sites

Hi there! I see you're using React! Also we can't really help you with just some code snippet please provide a minimal demo!  And read the following when working with React.

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

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

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

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

 

 Side note split from 

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