Jump to content
Search Community

How to make my smooth scroll a bit not laggy

osama00785 test
Moderator Tag

Recommended Posts

Hi there, hope everyone is doing well

I've an issue using gsap, scrollTrigger to be more specific for smooth scrolling (horizontal and vertical) currently everything is working well. The only issue i'm having is the lag that can be hardly noticed at the start of the page on desktop, but its really noticed on mobile.

Here is the website link: https://beyond-the-veil.vercel.app
Here is my code bellow, i've sent my whole code because it is really simple that work for vertical (mobile) and horizontal (desktop) and i'm having a bit of lag on both, thanks in advance for whoever helps.
 

import { useRef, useState, useEffect } from "react";

import gsap from "gsap";
import ScrollTrigger from "gsap/dist/ScrollTrigger";

gsap.registerPlugin(ScrollTrigger);

const HorizontalScrollComponent = () => {
  const component = useRef();
  const slider = useRef(null);
  const [isVerticalScroll, setIsVerticalScroll] = useState(false);

  useEffect(() => {
    const handleResize = () => {
      if (window.innerWidth < 1024) {
        setIsVerticalScroll(true);
      } else {
        setIsVerticalScroll(false);
      }
    };

    window.addEventListener("resize", handleResize);
    handleResize(); // Initial check

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

  useEffect(() => {
    // Target the container element to be scrolled
    const container = slider.current;
    const panels = gsap.utils.toArray(".panel");

    const scrollDirection = isVerticalScroll ? "y" : "x";
    const scrollSize = panels.reduce(
      (acc, panel) =>
        acc + (isVerticalScroll ? panel.offsetHeight : panel.offsetWidth),
      0
    );

    gsap.to(panels, {
      [scrollDirection]: () =>
        -scrollSize + window[isVerticalScroll ? "innerHeight" : "innerWidth"],
      ease: "linear",
      scrollTrigger: {
        trigger: container,
        pin: true,
        scrub: isVerticalScroll ? 2 : 3.5,
        start: "top top",
        end: `+=${
          scrollSize + window[isVerticalScroll ? "innerHeight" : "innerWidth"]
        }`,
      },
    });

    // Clean up ScrollTrigger when the component unmounts
    return () => {
      ScrollTrigger.getAll().forEach((trigger) => trigger.kill(true));
    };
  }, [isVerticalScroll]);



  return (
    <>
      <div ref={component}>
        <div
          ref={slider}
          className="app flex flex-col h-[100vh] w-[100vw] lg:flex-row lg:flex-nowrap lg:overflow-hidden"
        >
          <div
            id="header"
            className="panel w-full lg:h-[100vh] -z-[10] relative"
          >
            <LandingPage />
          </div>
          <div
            id="tracking"
            className="panel lg:h-[100vh] sbg-red-500 pt-[300px] md:pt-[250px] lg:pt-0"
          >
            <Tracking />
          </div>
          <div
            id="investigating"
            className="panel lg:h-[100vh] sbg-blue-500 pt-[300px] md:pt-[250px] lg:pt-0"
          >
            <Investigating />
          </div>
          <div
            id="revealing"
            className="panel lg:h-[100vh] sbg-green-500 pt-[300px] md:pt-[250px] lg:pt-0"
          >
            <Revealing />
          </div>
          <div
            id="unveiling"
            className="panel lg:h-[100vh] sbg-purple-500 pt-[300px] md:pt-[250px] lg:pt-0"
          >
            <Unveiling />
          </div>
          <div
            id="footer"
            className="panel lg:h-[100vh] sbg-red-500 pt-[300px] md:pt-[250px] lg:pt-0"
          >
            <Footer />
          </div>
        </div>
      </div>
    </>
  );
};

export default HorizontalScrollComponent;

 

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, 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: 

Important read this article when using React! 

 

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