带时间线的 Gsap 滚动触发器

发布于 2025-01-16 22:41:15 字数 646 浏览 1 评论 0原文

我正在使用 gsap 制作 React 应用程序。我在时间轴中使用滚动触发器,但它不起作用。滚动触发器无法工作。有人可以帮助我吗?这是我的代码

gsap.registerPlugin(ScrollTrigger); const el = useRef(null); const q = gsap.utils.selector(el);

useEffect(() => { 让 tl = gsap.timeline({ 滚动触发器:{ 触发:q(".scrollDist"), 开始:“顶顶”, 结束:“底部中心”, 擦洗:1, 标记:true, <代码>}, });

tl.fromTo(
  q(".header"),
  { y: 0, opacity: 1 },
  { y: -100, opacity: 0 }
).fromTo(".button_field", { y: 0 }, { y: -50 });

}, []);

I'm making react app using gsap. I use scrolltrigger in timeline but it's not working. The scrolltrigger couldn't work.Can someone help me? Here is my code

gsap.registerPlugin(ScrollTrigger);
const el = useRef(null);
const q = gsap.utils.selector(el);

useEffect(() => {
let tl = gsap.timeline({
scrollTrigger: {
trigger: q(".scrollDist"),
start: "top top",
end: " bottom center",
scrub: 1,
markers: true,
},
});

tl.fromTo(
  q(".header"),
  { y: 0, opacity: 1 },
  { y: -100, opacity: 0 }
).fromTo(".button_field", { y: 0 }, { y: -50 });

}, []);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是我的挚爱i 2025-01-23 22:41:15

如果没有正确的代码片段,有点难以理解您想要实现的目标,但我会破解的。

乍一看,您似乎需要将 scrub: 1 配置更改为 scrub: true

另一个问题是它可能无法正确查询元素,如果不看到标记就很难判断。

这是对 React 组件中完整代码的假设。

import React, { useEffect, useRef } from 'react'
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/all'

gsap.registerPlugin(ScrollTrigger);

const IndexPage = () => { {
   const elementRef = useRef(null)
   const q = gsap.utils.selector(elementRef)

    useEffect(() => {
      let tl = gsap.timeline(
        { 
          scrollTrigger: {
            trigger: q(".scrollDist"),
            start: "top top",
            end: "bottom center",
            scrub: true, // scrub: 1
            markers: true
          }
        }
      );

      tl
        .fromTo(
          q(".header"),
          { y: 0, opacity: 1 },
          { y: -100, opacity: 0 }
        )
        .fromTo(
          ".button_field",
          { y: 0 },
          { y: -50 }
        );
    }, [])

    return (
      <div ref={elementRef}>
        <div className='scrollDist'></div>
        <header className='header'>header</header>
        <button className='button_field'>button</button>
      </div>
    )
}

export default IndexPage

A tad hard to pick up on what you're trying to achieve without a proper code snippet but I'll have a crack.

First glance it looks like you need to change the scrub: 1 config to scrub: true.

The other concern is it may not be querying for the elements properly, hard to tell without seeing the markup.

This is an assumption of the full code you have within your React component.

import React, { useEffect, useRef } from 'react'
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/all'

gsap.registerPlugin(ScrollTrigger);

const IndexPage = () => { {
   const elementRef = useRef(null)
   const q = gsap.utils.selector(elementRef)

    useEffect(() => {
      let tl = gsap.timeline(
        { 
          scrollTrigger: {
            trigger: q(".scrollDist"),
            start: "top top",
            end: "bottom center",
            scrub: true, // scrub: 1
            markers: true
          }
        }
      );

      tl
        .fromTo(
          q(".header"),
          { y: 0, opacity: 1 },
          { y: -100, opacity: 0 }
        )
        .fromTo(
          ".button_field",
          { y: 0 },
          { y: -50 }
        );
    }, [])

    return (
      <div ref={elementRef}>
        <div className='scrollDist'></div>
        <header className='header'>header</header>
        <button className='button_field'>button</button>
      </div>
    )
}

export default IndexPage
浅笑依然 2025-01-23 22:41:15

我正在使用此代码片段构建时间线

const myTimeline = useRef(
    gsap.timeline({
      defaults: {
        ease: "none",
      },
    })
  );

并使用 const tl = myTimeline.current;

I am using this snippet to build a timeline

const myTimeline = useRef(
    gsap.timeline({
      defaults: {
        ease: "none",
      },
    })
  );

And use const tl = myTimeline.current;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文