Having trouble with GSAP Scrolltrigger PIN functionality in Next.js?

When I comment out the pin: true property in my code snippet below, everything works as expected except that the container wrapping the sections that should scroll horizontally does not stick to the top. However, if I uncomment the pin: true line, the entire container (trigger) becomes invisible.

I would greatly appreciate any suggestions on how to resolve this issue.

import React, { useEffect } from "react";
import OverlayMenu from "./OverlayMenu";
import { gsap } from "gsap";
import ScrollTrigger from "gsap/dist/ScrollTrigger";

function MainContent({ overlayRef }) {
  gsap.registerPlugin(ScrollTrigger);
  useEffect(() => {
    // alert(document.querySelector(".main__content").offsetWidth)
    const sections = gsap.utils.toArray(".section");
    gsap.to(sections, {
      xPercent: -100 * (sections.length - 1),
      ease: "none",
      scrollTrigger: {
        trigger: ".main__content",
        scrub: 1,
        markers: true,
        start: "top top",
        // // snap: 1 / (sections.length - 1),
        end: "+=" + document.querySelector(".main__content").offsetWidth,
        pin: true,
      },
    });
  }, []);

  return (
    <div className="main__content__wrapper w-[calc(100%_-_80px)] h-screen ml-20">
      <div className="w-full relative h-screen">
        <OverlayMenu overlayRef={overlayRef} />
        {/* <div className="w-full h-screen bg-black"></div> */}
        <div className="main__content w-[300%] bg-purple-700 h-screen flex  flex-nowrap">
          <div className="section w-full h-screen- bg-red-500">1</div>
          <div className="section w-full h-screen- bg-blue-500">2</div>
          <div className="section w-full h-screen- bg-yellow-500">3</div>
        </div>
      </div>
    </div>
  );
}

export default MainContent;

Answer №1

Go back to version 3.8.0

yarn add <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b3a7b5a494e7faecfae4">[email protected]</a>

OR

npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4c584a5b6b180513051b">[email protected]</a>

For more information, check out:

Answer №2

import React, { useEffect, useState } from "react";
import OverlayMenu from "./OverlayMenu";
import { gsap } from "gsap";
import ScrollTrigger from "gsap/dist/ScrollTrigger";

function MainContent({ overlayRef }) {
  gsap.registerPlugin(ScrollTrigger);
  const [hasRendered, setHasRendered] = useState(false);
  useEffect(() => {
    // Keeping track of rendering steps by setting the hasRendered state when the page renders
    setHasRendered(true);
  }, []);

  useEffect(() => {
    // Create gsap timeline instance only after page has rendered
    if (hasRendered) {
      const sections = gsap.utils.toArray(".section");
      gsap.to(sections, {
        xPercent: -100 * (sections.length - 1),
        ease: "none",
        scrollTrigger: {
          trigger: ".main__content",
          scrub: 1,
          markers: true,
          start: "top top",
          end: "+=" + document.querySelector(".main__content").offsetWidth,
          pin: true,
        },
      });
    }
  }, [hasRendered]);

  return (
    <div className="main__content__wrapper w-[calc(100%_-_80px)] h-screen ml-20">
      <div className="w-full relative h-screen">
        <OverlayMenu overlayRef={overlayRef} />
        <div className="main__content w-[300%] bg-purple-700 h-screen flex  flex-nowrap">
          <div className="section w-full h-screen- bg-red-500">1</div>
          <div className="section w-full h-screen- bg-blue-500">2</div>
          <div className="section w-full h-screen- bg-yellow-500">3</div>
        </div>
      </div>
    </div>
  );
}

export default MainContent;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The footer seems to detach on certain pages

Currently, I am facing a frustrating issue while working on a website. The footer seems to be disconnected from the main body content on certain pages, while on others, it appears to be properly attached. Despite my efforts to troubleshoot by removing elem ...

Incorporating Typescript with Chart.js: The 'interaction.mode' types do not match between these two entities

I am working on developing a React Functional Component using Typescript that showcases a chart created with chartjs. The data and options are passed from the parent component to the child component responsible for rendering the line chart. During the pr ...

Combining Node and React: Using CSS Class Names with Hyphens

Currently, my project utilizes react-starter-kit and includes several react components, each with its own styling file (scss). Every component imports the styling file at the top like this: import s from './Header.scss'; When it comes to using ...

Hovering over a white background will darken the CSS styling

A stunning image on a dark background Displayed on Hover (right) An elegant picture with a white background Activated on hover (incorrectly) I would like it to have a darkened effect on the white background as well. CSS Styling .photo background: n ...

Develop a personalized Hook for form submission in React

I find myself repeating the same process multiple times in my project every time I submit a form. This involves displaying a loader, removing it, and then displaying errors or success messages. Is there a way to create a customized hook to streamline this ...

Place form at the center of the Bootstrap Modal

My question is, how can I center the entire form, including the input fields and labels, within the modal? Here is also a helpful Fiddle: http://example.com, although the snippet provided should work as well. <script src="https://example.com/bootstr ...

Navigation bar collapse items are not properly aligned in the layout

My navbar collapse button is not aligning the items in the dropdown properly. I suspect it has to do with the way I have structured the divs. How can this issue be fixed? <nav class="navbar navbar-expand-md navbar-light mb-4 row"> <b ...

What is the functionality of the basePath feature in Next.js when it comes to managing

I have encountered an issue while trying to incorporate a basePath into my next.config.js. Although it works perfectly for the website's content, all the images appear broken. // next.config.js module.exports = { basePath: '/my-sub-url', ...

Using FEWER loops to create column classes on Twitter - What is their functionality?

Bootstrap utilizes various LESS mixins to create its column classes, along with other classes; .make-grid-columns() { // Common styles for all sizes of grid columns, widths 1-12 .col(@index) when (@index = 1) { // initial @item: ~".col-xs-@{index} ...

What is the best way to fix the Syntax error that reads "Unexpected token (1:13)"?

I can't seem to figure out why my code keeps showing errors in the browser. I'm still new to coding and learning slowly, with help from knowledgeable individuals on stackoverflow :) Card 1.jsx Syntax error:() Unexpected token (1:13) > 1 | i ...

Styling images and text in CSS within a jQuery UI autocomplete widget

I am currently using an autocomplete widget that displays text along with images. The results I have right now are not meeting my requirements. I want to customize the appearance of my results so that the words 'test' and either 'Federico&a ...

Within the .module.scss file, there exist multiple classes that are identical; however, only one classname is reflected

How can I differentiate elements with the same class name? :S In Next.js, only one column1 is generated for each end cell1, cell2 class, etc.. Is there a way to solve this??: className={styles.left.column1}, className={styles.left.column1.cell1}, className ...

Using CSS to leverage the power of both Grid and Flex simultaneously

Help Needed: CSS Challenge! I'm not a fan of CSS and can't seem to crack this code conundrum. Here's what I want the end result to look like: https://i.sstatic.net/z06qO.png Current Situation: #newOrderControl { border-style: solid ...

More content will not be displayed beneath folded DIVs arranged to stack on mobile devices

On my website, I have two separate sections dedicated to use cases and benefits. Here is the code: .onboardmessaging { min-width: 100%; } .onboardmessagingwrap { min-width: 100%; } .usecase { padding-left: 8vw; max-widt ...

Could someone shed some light on why my code within the useEffect hook in my component is being triggered twice when it shouldn't be?

I'm currently developing a VR Web project using Three.js within a React-Vite-Typescript setup. As I begin this new project, my focus is on coding the initial view. It starts with a black screen displaying a white logo at the center, reminiscent of in ...

My Next.js application is facing an issue where process.env.NODE_ENV is not being properly configured

The issue I'm facing involves my app. I have an .env.local file where I've set the NODE_ENV to development. However, when I check the logs, it shows a value of production. There are no other locations in my app where NODE_ENV is referenced. To r ...

Animating skill bars as you scroll down the page

How can I achieve a skill bar animation on a specific section of the page scroll? I have attempted to create a sliding skill bar animation, but it is not producing the desired effect. It currently starts animating at the beginning of the page, but I want i ...

Using React-Testing-Library to Jest TestBed Hook in TypeScript for Jest Testing

I'm currently facing a challenge while attempting to integrate the react-hooks library with Formik, specifically using useFormikContext<FormTypeFields>() in TypeScript within my project. I have certain fields where I want to test the automation ...

Tips for recalling the display and concealment of a div element using cookies

My HTML code looks like this: <div id='mainleft-content'>content is visible</div> <div id="expand-hidden">Button Expand +</div> To show/hide the divs, I am using JQuery as shown below: $(document).ready(function () { ...

Disable the movement and dragging functionality of the scroll feature in Google Maps using React

I have a map.jsx file in my React application that contains the code below: import React from 'react'; import GoogleMapReact from 'google-map-react'; import './map.css'; const Map = ({ location, zoomLevel }) => ( <d ...