【发布时间】:2021-04-15 23:39:48
【问题描述】:
所以我只在 ReactJS 工作了几个月,所以如果解决方案很简单,请见谅。
一旦所需的部分在视口中,我正在使用 react-use 中的交叉点观察器触发 gsap 动画。现在我不希望它动画出来,只动画(在初始加载时)。我按照教程进行操作,但他们的解释只是注释掉淡出动画,这对我不起作用。
我的代码如下所示:
const App = () =>{
const sectionRef = useRef(null);
const headlineFirst = ".animate-this-div"
const intersection = useIntersection(sectionRef, {root: null,rootMargin:
"0px",threshold: 0.4});
const fadeIn = () => {gsap.to(headlineFirst, 1, {opacity: 1,y:11,})};
const fadeOut = () => {
// gsap.to(headlineFirst , 1, {
// opacity: 1,
// y:49,
// })
};
intersection && intersection.intersectionRatio < 0.4
? fadeOut()
: fadeIn(headlineFirst);
return (
<div ref={ sectionRef } className="some-div">
<div className="animate-this-div"></div>
</div>
应该是三元运算符吧。一个“if 语句”会起作用吗,那个“if 语句”看起来会如何(对不起,在 ReactJS 上仍然是一个菜鸟)?任何建议都会非常感谢
【问题讨论】:
-
为什么不试试 GSAP 的 Scroll Trigger 插件?:greensock.com/scrolltrigger
-
感谢@Rodrigo 的回复。GSAP 是否向 scrollTrigger 添加了交错动画?
标签: reactjs gsap intersection-observer