reactjs - 如何使用 Gsap 在 gartsby 中的页面之间添加动画
问题描述
我想使用 Gsap 在 gatsby 的页面之间添加动画。
在某些时候他们说:最后,在任何你想使用它的地方导入 TransitionLink 组件...... https://www.gatsbyjs.com/docs/how-to/adding-common-features/adding-page-transitions-with -plugin-transition-link/#getting-started
但是我不知道在哪里使用,应该在实际页面组件中使用还是在布局组件中使用?(我希望每个页面都有不同的动画)
我想使用触发功能: https ://www.gatsbyjs.com/docs/how-to/adding-common-features/adding-page-transitions-with-plugin-transition-link/#using-the-trigger -function 因为我想使用 Gsap。
但后来他们只是写在代码“someCustomDefinedAnimation”中,但我不知道它来自哪里,我如何创建一个以及如何通过道具传递它。
<TransitionLink
exit={{
length: length,
trigger: ({ exit, node }) =>
this.someCustomDefinedAnimation({ exit, node, direction: "out" }),
}}
entry={{
length: 0,
trigger: ({ exit, node }) =>
this.someCustomDefinedAnimation({ exit, node, direction: "in" }),
}}
{...props}
>
{props.children}
</TransitionLink>
所以我的问题是:
- 我应该在哪里添加 TransitionLink 组件?
- 如何创建自定义 Gsap 动画?
- 我在哪里以及如何将动画作为道具传递,以便 TransitionLink 可以使用它?
解决方案
但是他们只是写在代码中,
someCustomDefinedAnimation
但我不知道它来自哪里,我如何创建一个以及如何通过道具传递它。
好吧,someCustomDefinedAnimation
只是一个包含动画的定义函数。例如:
verticalAnimation = ({ length, direction}) => {
const directionTo = direction === 'up' ? '-100%' : '100%'
const directionFrom = direction === 'up' ? '100%' : '-100%'
// convert ms to s for gsap
const seconds = length
return gsap.timeline()
.set(this.transitionCover, { y: directionFrom })
.to(this.transitionCover, {
y: '0%',
ease: "power1.easeInOut",
duration: seconds / 2,
})
.set(this.layoutContents, { opacity: 0 })
.to(this.transitionCover, {
y: directionTo,
ease: "power1.easeIn",
duration: seconds / 2,
})
}
然后,您需要将其用作:
<TransitionLink
exit={{
length: length,
trigger: ({ exit, node }) =>
this.verticalAnimation({length: 100, direction: 'up'}),
}}
entry={{
length: 0,
trigger: ({ exit, node }) =>
this.verticalAnimation({length: 100, direction: "down" }),
}}
{...props}
>
{props.children}
</TransitionLink>
基本上,它允许您完全自定义在每个页面转换中触发的功能
您可以在他们的GitHub 存储库中的TransitionLink
文档和实现示例中找到详细的解释和演练。
推荐阅读
- javascript - 如何在asp.net中使用ScriptManager从aspx代码访问外部JS文件?
- python - 使用服务帐户通过 python 访问文件
- python - 补丁移动:文件备份和替换
- spring-boot - 具有多个数据源的 Spring Boot + Camel JPA
- javascript - 日期选择器持续时间问题
- android - 有没有办法检查哪个清单导入了一些使用权限?
- scala - 何时应该使用 Scala 类以及何时在 IDE 中使用 Scala 对象?
- python - Python多线程/进程用值填充矩阵
- qweb - 删除货币符号 odoo 11
- ruby-on-rails - 如何传递参数以使 sidekiq 中的作业过期