GSAP ScrollTrigger在Next.js中无法正常工作。

huangapple go评论97阅读模式
英文:

GSAP Scrolltrigger PIN not working properly in nextjs

问题

从我的下面的代码来看,如果我注释掉pin: true属性,代码会正常工作,但包装部分我希望水平滚动的容器不会固定在顶部。如果我取消注释pin: true,所有容器(触发器)将不可见。

有关如何解决此问题的任何建议将不胜感激。

英文:

From my code below, If I comment out the pin: true property, the code works normally but the container that wraps the sections I expect to scroll horizontally are not sticky to the top. If I uncomment the pin: true, all the container (trigger) will not be visible.

Any suggestions on how to resolve this issue will be greatly appreciated.

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;

答案1

得分: 1

将版本降级到 3.8.0

yarn add gsap@3.8.0

或者

npm install gsap@3.8.0

你可以在这里阅读更多信息:
https://greensock.com/forums/topic/30747-scrolltrigger-in-nextjs-build-not-working-reliably/#comment-153675

英文:

Step back the version to 3.8.0

yarn add gsap@3.8.0

OR

npm install gsap@3.8.0

You can read more here:
https://greensock.com/forums/topic/30747-scrolltrigger-in-nextjs-build-not-working-reliably/#comment-153675

答案2

得分: 0

以下是您提供的代码的翻译部分:

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(() => {
    // ******This will set the state of hasRendered when the page render so as to keep track of the rendering steps *******
    setHasRendered(true);
  }, []);

  useEffect(() => {
    // ******* I added the conditional statement here to only create the instance of the gsap timeline ONLY after the page has rendered*******
    if (hasRendered) {
      // 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,
        },
      });
    }
  }, [hasRendered]); //******** Dont forget the dependencies array too */

  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;

请注意,翻译中的代码部分没有进行任何修改,仅提供了注释和字符串的翻译。如果您需要进一步的帮助,请随时提出。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React, { useEffect, useState } from &quot;react&quot;;
import OverlayMenu from &quot;./OverlayMenu&quot;;
import { gsap } from &quot;gsap&quot;;
import ScrollTrigger from &quot;gsap/dist/ScrollTrigger&quot;;
function MainContent({ overlayRef }) {
gsap.registerPlugin(ScrollTrigger);
const [hasRendered, setHasRendered] = useState(false);
useEffect(() =&gt; {
// ******This will set the state of hasRendered when the page render so as to keep track of the rendering steps *******
setHasRendered(true);
}, []);
useEffect(() =&gt; {
// ******* I added the conditional statement here to only create the instance of the gsap timeline ONLY after the page has rendered*******
if (hasRendered) {
// alert(document.querySelector(&quot;.main__content&quot;).offsetWidth)
const sections = gsap.utils.toArray(&quot;.section&quot;);
gsap.to(sections, {
xPercent: -100 * (sections.length - 1),
ease: &quot;none&quot;,
scrollTrigger: {
trigger: &quot;.main__content&quot;,
scrub: 1,
markers: true,
start: &quot;top top&quot;,
// // snap: 1 / (sections.length - 1),
end: &quot;+=&quot; + document.querySelector(&quot;.main__content&quot;).offsetWidth,
pin: true,
},
});
}
}, [hasRendered]); //******** Dont forget the dependencies array too */
return (
&lt;div className=&quot;main__content__wrapper w-[calc(100%_-_80px)] h-screen ml-20&quot;&gt;
&lt;div className=&quot;w-full relative h-screen&quot;&gt;
&lt;OverlayMenu overlayRef={overlayRef} /&gt;
{/* &lt;div className=&quot;w-full h-screen bg-black&quot;&gt;&lt;/div&gt; */}
&lt;div className=&quot;main__content w-[300%] bg-purple-700 h-screen flex  flex-nowrap&quot;&gt;
&lt;div className=&quot;section w-full h-screen- bg-red-500&quot;&gt;1&lt;/div&gt;
&lt;div className=&quot;section w-full h-screen- bg-blue-500&quot;&gt;2&lt;/div&gt;
&lt;div className=&quot;section w-full h-screen- bg-yellow-500&quot;&gt;3&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
);
}
export default MainContent;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 01:47:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定