Gsap滚动触发,pin属性在网页中留白并隐藏该部分

问题描述 投票:0回答:1

我正在通过 pin 属性在 gsap 中进行水平滚动,如果我从上到下滚动,图像和所有内容都会被隐藏,但如果我转到网站底部并从下到上滚动,它正在工作吗?

我正在使用nextjs

import { gsap } from "gsap";
import React, { useLayoutEffect } from "react";

const GallerySection = () => {
  useLayoutEffect(() => {
    const horizontal = document.querySelector("#container");
    const totalWidth = horizontal.offsetWidth * 4 - window.innerWidth;
    console.log(totalWidth);

    const tl = gsap.timeline({
      scrollTrigger: {
        trigger: "#container",
        markers: true,
        start: "top top",
        scrub: true,
        pin: true,
        pinSpacing: false,
        pinnedContainer: false,
        pinSpacer: false,
      },
    });

    tl.to("#wrapper", { x: -totalWidth });
    // tl.kill();
    () => tl.kill();
  }, []);
  return (
    <div id="container" className="h-[100vh] w-full">
      <div
        id="wrapper"
        className="h-[100vh] w-full bg-green-500 flex justify-between items-center "
      >
        {/* 1 */}
        <div>
          <div className="h-[100vh]  flex-col bg-green-500 w-[100vw] flex items-center justify-center relative">
            <div
              className="w-[840px]  h-[600px] "
              style={{
                objectFit: "contain",
              }}
            >
              <img
                className="w-full h-full "
                style={{
                  objectFit: "fill",
                }}
                src="https://images.unsplash.com/photo-1566204773863-cf63e6d4ab88?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1345&q=100"
                alt=""
              />
            </div>

            <div className="absolute left-[10%] bottom-[25%]">
              <h1 className="text-[50px] ">Dracaena Trifasciata</h1>
              <h2 className="text-[30px] ">Live the Beauty</h2>
            </div>
          </div>
        </div>
        {/* 2 */}
        <div>
          <div className="h-[100vh]  flex-col bg-green-500 w-[100vw] flex items-center justify-center relative">
            <div
              className="w-[840px]  h-[600px] "
              style={{
                objectFit: "contain",
              }}
            >
              <img
                className="w-full h-full "
                style={{
                  objectFit: "fill",
                }}
                src="https://images.unsplash.com/photo-1558603668-6570496b66f8?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1300&q=100"
                alt=""
              />
            </div>

            <div className="absolute left-[10%] bottom-[25%]">
              <h1 className="text-[50px] ">Cereus Penuvianus</h1>
              <h2 className="text-[30px] ">Live the Beauty</h2>
            </div>
          </div>
        </div>
        {/* 3 */}
        <div>
          <div className="h-[100vh]  flex-col bg-green-500 w-[100vw] flex items-center justify-center relative">
            <div
              className="w-[840px]  h-[600px] "
              style={{
                objectFit: "contain",
              }}
            >
              <img
                className="w-full h-full "
                style={{
                  objectFit: "fill",
                }}
                src="https://images.unsplash.com/photo-1567225557594-88d73e55f2cb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=934&q=100"
                alt=""
              />
            </div>

            <div className="absolute left-[10%] bottom-[25%]">
              <h1 className="text-[50px] ">Calliope</h1>
              <h2 className="text-[30px] ">Live the Beauty</h2>
            </div>
          </div>
        </div>
        {/* 4 */}
        <div>
          <div className="h-[100vh]  flex-col bg-green-500 w-[100vw] flex items-center justify-center relative">
            <div
              className="w-[840px]  h-[600px] "
              style={{
                objectFit: "contain",
              }}
            >
              <img
                className="w-full h-full "
                style={{
                  objectFit: "fill",
                }}
                src="https://images.unsplash.com/photo-1611145367651-6303b46e4040?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2006&q=100"
                alt=""
              />
            </div>

            <div className="absolute left-[10%] bottom-[25%]">
              <h1 className="text-[50px] ">Golden Pothos</h1>
              <h2 className="text-[30px] ">Live the Beauty</h2>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};

export default GallerySection;

此图像是如果我从上到下滚动,则不起作用

这张图片是如果我从下到上滚动,工作

reactjs next.js gsap scrolltrigger
1个回答
0
投票

尝试在可用选项中输入“end”。

scrollTrigger: {
  trigger: container,
  scrub: true,
  start: "top 80%",
  end: "top 55%",
  immediateRender: false,
  markers: true,
},
© www.soinside.com 2019 - 2024. All rights reserved.