所以我使用 React/ PoseShowcase
来为一个组件制作动画,比如这里看到的Postmates 车队网站:
我快到了,但我不知道最后一点。基本上,我有一系列项目:
const chapters = [
{
content: "1",
title: "Create your profile"
},
{
content: "2",
title: "See their rating"
},
{
content: "3",
title: "Forgot annoying requests"
},
{
content: "4",
title: "Block them"
},
{
// tslint:disable-next-line:max-line-length
content: "5",
title: "Make your decision"
},
{
content: "6",
title: "Time elapses"
},
{
content: "7",
title: "Complete"
}
];
还有一种handleScroll(e)
方法可以做一些事情:
- 在组件中滚动时检测
- 滚动一章时,它会更新
this.state.content
并this.state.title
使用正确的章节内容来自chapters
:
--
// loop through the chapters
for (let index = 0; index < chapters.length; index++) {
const chapterHeight = componentHeight;
topOfChapter.push(componentTopY + chapterHeight * index);
bottomOfChapter.push(componentTopY * 2 + chapterHeight * index);
// when scrolling through the component
if (
scrollY >= component.offsetTop &&
scrollY <= component.offsetHeight + window.innerHeight
) {
// when scrolling through a chapter
if (
scrollY >= topOfChapter[index] &&
scrollY <= bottomOfChapter[index]
) {
// tslint:disable-next-line:no-console
console.log(`Scrolling through chapter ${index + 1}`);
this.setState({
animate: !this.state.animate,
content: chapters[index].content,
title: chapters[index].title
});
// tslint:disable-next-line:no-console
console.log(topOfChapter[index], "topOfChapter[index]");
// tslint:disable-next-line:no-console
console.log(bottomOfChapter[index], "bottomOfChapter[index]");
}
} else {
// tslint:disable-next-line:no-console
console.log("exited the component");
}
}
问题是我的动画总是在触发,因为this.state.animate
滚动时总是在变化。
我只需要在章节更改时触发动画,而不是一直滚动,但我不知道如何。
几个小时前我问了一个问题,它没有动画,但我认为这个问题只有在动画的背景下才真正有意义。
非常感谢任何帮助!