next.js 帧运动|在Y上滚动X

rqdpfwrv  于 2023-08-04  发布在  其他
关注(0)|答案(1)|浏览(76)

目标是将一个部分显示在视图中,然后暂停并滚动该部分中存在的水平滚动条的全长,然后继续到下一个部分。我已经能够实现滚动水平滚动条,但它是链接到整个页面的Y滚动,而不是生效&完成时,只有容器是在视图中。
以下是我的代码:

export default function TechStack({ darkMode }: TechStackProps) {
  const targetRef = useRef(null);
  const { scrollYProgress } = useScroll();
  const translateElement = useTransform(
    scrollYProgress,
    [0.1, 0.8],
    ["0%", "-57%"]
  );
  const opacitySection = useTransform(
    scrollYProgress,
    [0.1, 0.7, 0.9],
    [0, 0, 1]
  );

  return (
    <section ref={targetRef}>
      <div
        style={{
          // opacity: opacitySection,
          zIndex: 20,
        }}
        className={`relative ${
          darkMode ? "dark" : `light`
        } flex flex-col items-center justify-center lg:items-start pt-[30%] md:pt-40 pb-20 md:w-full lg:h-screen lg:pb-40 `}
      >
        <div className="max-w-5xl p-4 z-20 lg:pl-[4rem] sticky top-20">
          <h1 className="font-black text-6xl px-4 py-4">
            DESIGN.
            <br />
            DEVELOP.
            <br />
            TEST.
          </h1>
          <p className="font-light text-2xl leading-1 p-4">
            Building a good digital experience requires an even better tool-set
            of technologies. These are some of the technologies I have worked
            with recently.
          </p>
        </div>
        <motion.div style={{ x: translateElement }} className="translate-this-div-on-scroll">
          <div className="tech-stack w-full flex flex-nowrap px-[2rem] md:px-[5rem] gap-8 py-8 z-20 overflow-hidden">
            <TechContainer
              darkMode={darkMode}
              technologyCategory="FRONT-END DEVELOPMENT"
              technologyData={frontEndTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="BACK-END DEVELOPMENT"
              technologyData={backEndTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="DATABASE MANAGEMENT"
              technologyData={databaseTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="SERVERS"
              technologyData={serverTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="VERSION CONTROL"
              technologyData={versionControlTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="DESIGN"
              technologyData={designTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="IDE"
              technologyData={IDE}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="OTHER TECHNOLOGIES"
              technologyData={otherTechnologies}
            />
          </div>
        </motion.div>
      </div>
    </section>
  );
}

字符串

toiithl6

toiithl61#

这是我的解决方案,希望对你有所帮助。

import { motion, useTransform } from "framer-motion";
import { useInView } from "react-intersection-observer";

export default function TechStack({ darkMode }: TechStackProps) {
  const [ref, inView] = useInView({
    threshold: 0.5, // set the threshold to 0.5, which means the container is considered in view when it's 50% visible
    triggerOnce: true, // only trigger the inView callback once
  });
  const translateElement = useTransform(
    scrollXProgress,
    [0, 1],
    ["0%", "-100%"]
  );

  return (
    <section ref={ref}>
      <div
        style={{
          opacity: inView ? 1 : 0, // set the opacity to 1 when the container is in view, otherwise set it to 0
          zIndex: 20,
        }}
        className={`relative ${
          darkMode ? "dark" : `light`
        } flex flex-col items-center justify-center lg:items-start pt-[30%] md:pt-40 pb-20 md:w-full lg:h-screen lg:pb-40 `}
      >
        <div className="max-w-5xl p-4 z-20 lg:pl-[4rem] sticky top-20">
          <h1 className="font-black text-6xl px-4 py-4">
            DESIGN.
            <br />
            DEVELOP.
            <br />
            TEST.
          </h1>
          <p className="font-light text-2xl leading-1 p-4">
            Building a good digital experience requires an even better tool-set
            of technologies. These are some of the technologies I have worked
            with recently.
          </p>
        </div>
        <motion.div style={{ x: translateElement }} className="translate-this-div-on-scroll">
          <div className="tech-stack w-full flex flex-nowrap px-[2rem] md:px-[5rem] gap-8 py-8 z-20 overflow-hidden">
            <TechContainer
              darkMode={darkMode}
              technologyCategory="FRONT-END DEVELOPMENT"
              technologyData={frontEndTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="BACK-END DEVELOPMENT"
              technologyData={backEndTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="DATABASE MANAGEMENT"
              technologyData={databaseTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="SERVERS"
              technologyData={serverTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="VERSION CONTROL"
              technologyData={versionControlTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="DESIGN"
              technologyData={designTechnologies}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="IDE"
              technologyData={IDE}
            />
            <TechContainer
              darkMode={darkMode}
              technologyCategory="OTHER TECHNOLOGIES"
              technologyData={otherTechnologies}
            />
          </div>
        </motion.div>
      </div>
    </section>
  );
}

字符串

相关问题