我想在 Nextjs 中使用 GSAP 暂停/开始动画。我找不到在 useEffect 挂钩之外使用 tl.current 的方法。在这个组件中,当我单击第一个 .imgWrapper 时,我希望 tl.current 切换 tl.current.paused(true) 并暂停动画。但我没办法让它工作
export const Projects = () => {
const projects = useRef();
const projectTitle = useRef(null);
const tl = useRef();
const q = gsap.utils.selector(projects);
useEffect(() => {
tl.current = gsap
.timeline()
.from(q(".singleProject:nth-child(2)"), {
y: 2000,
ease: Linear.easeNone,
opacity: 1,
duration: 20,
repeat: -1,
paused: false,
})
.to(q(".singleProject:nth-child(2)"), {
y: -2000,
ease: Linear.easeNone,
opacity: 1,
duration: 20,
repeat: -1,
paused: false,
});
tl.current = gsap
.timeline()
.from(q(".singleProject:nth-child(3)"), {
y: 2000,
ease: Linear.easeNone,
opacity: 1,
duration: 20,
repeat: -1,
delay: 1,
})
.to(q(".singleProject:nth-child(3)"), {
y: -2000,
ease: Linear.easeNone,
opacity: 1,
duration: 20,
repeat: -1,
delay: 1,
});
tl.current = gsap
.timeline()
.from(q(".singleProject:nth-child(4)"), {
y: 2000,
ease: Linear.easeNone,
opacity: 1,
duration: 20,
repeat: -1,
delay: 2,
})
.to(q(".singleProject:nth-child(4)"), {
y: -2000,
ease: Linear.easeNone,
opacity: 1,
duration: 20,
repeat: -1,
delay: 2,
});
}, []);
const [animate_1, setAnimate_1] = useState(false);
const [animate_2, setAnimate_2] = useState(false);
const [animate_3, setAnimate_3] = useState(false);
return (
<section
ref={projects}
id="projects"
className={`${animate_1 && "animated_1"} ${animate_2 && "animated_2"} ${
animate_3 && "animated_3"
}
w_100 h_100vh flex flexColumn flexAlignCenter flexCenter p4em`}
>
<div className="projectsLoading flex flexCenter flexAlignCenter w_100 h_100">
<h2>LOADING PROJECTS</h2>
</div>
<div className={`singleProject w_100 flex flexStart`}>
<div className="imgWrapper" onClick={() => some code}>
<img src={"https://www.aimanalmureish.com/img/lego.jpg"} />
</div>
</div>
<div className="singleProject w_100 flex flexEnd">
<div className="imgWrapper">
<img
onClick={() => {
setAnimate_2(!animate_2);
}}
src={"https://www.aimanalmureish.com/img/jordan.png"}
/>
</div>
</div>
<div className="singleProject w_100 flex flexStart">
<div className="imgWrapper">
<img
onClick={() => setAnimate_3(!animate_3)}
src={"https://www.aimanalmureish.com/img/ferrari.png"}
/>
</div>
</div>
</section>
);
};
这就是我 colsole.log(tl.current.pause()) 时控制台中显示的内容
https://i.stack.imgur.com/xo0LR.png
谢谢您的帮助