Jump to content
Search Community

I am using gsap and locomotive combined in react But this is the issue that I am facing It runs as per my expectations when I dont refresh from 5 mins but after refresh it does nothing I also used Locomotive scroll but with gsap scroll trigger config.i

lintarabail test
Moderator Tag

Recommended Posts

import React, { useEffect, useLayoutEffect, useRef } from "react";
import Navbar from "../../Components/Navbar";
import HeroSection from "../hero-section";
import SecondSection from "../SecondSection";
import ThirdSection from "../third-section";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import my from "../../assets/videos/my.mp4";
import MyProjects from "../projects-section";
import useLocoScroll from "../../utils/customHooks/useLocoMotive";
import { useState } from "react";

const Main = () => {
  // refs
  const ref = useRef(null);
  const img = useRef(null);
  // initialalizing custom hook
  useLocoScroll();
  // regiester ScrollTrigger
  gsap.registerPlugin(ScrollTrigger);
  
  useLayoutEffect(() => {
    let ctx = gsap.context(() => {
      const elem = ref.current;
      // for second section text
      gsap.to(
        elem.querySelector("#hid"),
        {
          transform: "translateX(-100%)",
          ease: "none",
          fontWeight: "40",
          scrollTrigger: {
            trigger: document.querySelector("#component"),
            markers: false,
            start: "start start",
            // end: "bottom 40%",
            // end: () => "+=" + document.querySelector("#hid").offsetWidth,
            end: () => "+=" + document.querySelector("#hid").offsetWidth * 0.1,
            scrub: 1,
            pin: true,
            fastScrollEnd: true,
          },
        },
        ref
      );
    });

    return () => {
      ctx.revert();
    };
  }, []);

  useLayoutEffect(() => {
    const ctx = gsap.context(() => {
      const elem = img.current;
      console.log();
      gsap.fromTo(
        "#header_img",
        { scale: 0.3, translateY: "-60%" },
        {
          // y: -10,
          scale: 1,
          ease: "none",
          force3D: true,
          scrollTrigger: {
            trigger: elem,
            pin: true,
            start: "top 65%",
            end: "bottom 70%",
            // pinType: isTouch ? "fixed" : "transform",
            scrub: 1,
            markers: true,
          },
        }
      );
    });

    return () => {
      ctx.revert();
    };
  }, []);

  return (
    <>
      <div ref={ref} id="scroll_head" data-scroll-container>
        <Navbar />
        <HeroSection />
        {/* second section */}
        <div
          className="flex justify-start items-center  overflow-hidden h-[100vh]"
          id="component"
          data-scroll-section
        >
          <h1
            className="text-[40vw] uppercase main-heading whitespace-nowrap py-10"
            id="hid"
          >
            Where Imagination Meets Code
          </h1>
          <p className="text-white text-4xl">njgjhghgt</p>
        </div>
        {/* Third section */}
        <div className="h-[60vh] flex justify-center items-center" ref={img} data-scroll-section>
         
          <img
            src="https://platinumlist.net/guide/wp-content/uploads/2023/03/IMG-worlds-of-adventure.webp"
            alt=""
            data-speed="0.5"
            id="header_img"
            style={{ transformOrigin: "center center", scale: "0.3" }}
          />
        </div>
        <MyProjects />
      </div>
    </>
  );
};

export default Main;
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/all";
import LocomotiveScroll from "locomotive-scroll";
import { useEffect } from "react";
import { Scroll } from "react-locomotive-scroll";

gsap.registerPlugin(ScrollTrigger);
const useLocoScroll = (start, ref) => {
  useEffect(() => {

    let locoScroll = new LocomotiveScroll({
      el: document.querySelector("[data-scroll-container]"),
      smooth: true,
      //   multiplier: 1,
      class: "is-reveal",
    });
    locoScroll.on("scroll", () => {
      ScrollTrigger.update();
    });
    ScrollTrigger.scrollerProxy(
      document.querySelector("[data-scroll-container]"),
      {
        scrollTop(val) {
          if (locoScroll) {
            return arguments.length
              ? locoScroll.scrollTo(val, 0, 0)
              : locoScroll.scroll.instance.scroll.x;
          }
          return null;
        },
        scrollLeft(val) {
          if (locoScroll) {
            return arguments.length
              ? locoScroll.scrollTo(val, 0, 0)
              : locoScroll.scroll.instance.scroll.x;
          }
          return null;
        },
      }
    );
    const isUpdate = () => {
      if (locoScroll) {
        locoScroll.update();
      }
    };
    ScrollTrigger.addEventListener("refresh", isUpdate);
    ScrollTrigger.refresh();
    return () => {
      if (locoScroll) {
        ScrollTrigger.removeEventListener("refresh", isUpdate);
        locoScroll.destroy();
        locoScroll = null;
      }
    };
  }, [start]);
};

export default useLocoScroll;
Link to comment
Share on other sites

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 Stackblitz that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. 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

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

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

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