javascript - React 和 Gsap 动画问题
问题描述
所以我正在尝试使用 Gsap 为我的反应应用程序制作动画我正在尝试使用滚动触发器创建动画当该部分进入视图时,标题应该移动。
我正在从我拥有的数据中呈现一个列表。
问题:标题为第一部分移动,但如果我延迟滚动,它不会为其他部分移动。意味着它只在第一部分移动。
我尝试过使用 gsap useEffect 方法,但它没有用,并且使用 Tween 组件方法仍然没有用。
import React, { useRef, useEffect } from "react";
import { homeData } from "./homedata";
import classes from "./MainContainer.module.css";
import { gsap } from "gsap";
import { Tween } from "react-gsap";
import { ScrollTrigger } from "gsap/dist/ScrollTrigger";
gsap.registerPlugin(ScrollTrigger);
const MainContainer = () => {
const boxRef = useRef(homeData.map(() => React.createRef()));
useEffect(() => {
// boxRef.current.forEach((ref, index) => {
// gsap.fromTo(
// ref.current,
// { y: 0 },
// {
// y: -100,
// duration: 2,
// ease: "bounce",
// scrollTrigger: {
// id: `section-${index}`,
// trigger: boxRef.current[index].current,
// start: "top center+=100",
// },
// }
// );
// console.log(ScrollTrigger);
// });
// ScrollTrigger.create({
// trigger: boxRef.current[2].current,
// onEnter: () => {
// console.log("Happened");
// },
// // onLeave: myLeaveFunc,
// // onEnterBack: myEnterFunc,
// // onLeaveBack: myLeaveFunc,
// });
// console.log(boxRef.current[0].current.focus);
// //
// gsap.to(boxRef.current[2].current, {
// y: -100,
// duration: 2,
// ease: "bounce",
// delay: 1,
// scrollTrigger: boxRef.current[2].current,
// });
}, []);
console.log(boxRef);
return (
<section>
{homeData.map((location, index) => {
return (
<section
id={`section-${index}`}
key={index}
className={classes["home-section"]}
style={{ background: `url(${location.image}) center/cover` }}
>
<div className={classes["left-side-main"]}>
<Tween
to={{
x: "300px",
scrollTrigger: {
trigger: `#heading${index}`,
},
}}
>
<h1 ref={boxRef.current[index]} id={`heading${index}`}>
{location.location}
</h1>
</Tween>
<div className={classes["previous-next"]}>
<button>Previous</button>
<span></span>
<button>Next</button>
</div>
</div>
<div className={classes["right-side-main"]}>
<p>{location.description}</p>
<div className={classes["images-right-side"]}>
<img src={location.moreImages[0]} alt="" />
<img src={location.moreImages[1]} alt="" />
</div>
</div>
</section>
);
})}
</section>
);
};
export default MainContainer;
解决方案
推荐阅读
- kotlin - 将带有后台任务的功能从主要活动转移到它自己的单独类中
- python - 循环迭代,返回最佳实践
- firebase - Firestore 中的 getDocuments() 和 snapshots() 有什么区别?
- javascript - lodash sortByOrder 没有按预期工作?
- mysql - 将一个巨大的选择查询分成几部分有意义吗?
- r - 根据特定列中唯一数量的值添加列
- react-native - 最新版本的 react-native 和 AsyncStorage 丢失数据
- python - Python Dataframe 中日期在 dd-mm-yyyy 中的日期差异
- r - modelssummary/kable 同名模型的额外回归表
- cloud - IBM Cloud 目前无法创建帐户