在array.map中使用Framer的UseAnimation钩
在过去的几天里,我一直在玩框架。目前,我正在尝试为一个3个图标组成的组动画。第一个动画是从底部淡入它们,每个图标都比以前更大。第二个动画是水平淡出它们。我已经设置了动画变体,例如以下代码:
const iconAnimationVariants = {
fadeIn: i => ({
opacity: [0, 1],
y: [20, 0],
transition: {
delay: i * 0.1,
duration: 0.6
}
}),
slideRight: i => ({
opacity: 0,
x: 20,
transition: {
delay: 2 + (i * 0.1),
duration: 1.6
}
})
}
接下来,我使用下面的代码将图标添加到我的应用程序中并为其添加动画。
const renderIcons = () =>
icons?.map(({src, alt}, index) => (
<motion.li
initial='hidden'
custom={index}
animate='fadeIn'
exit='slideRight'
variants={iconAnimationVariants}
className="intro-icon"
key={index}>
<img src={src} alt={alt} />
</motion.li>
))
这起作用了,至少fadein
变体工作的一部分。要有更多的控制,我想使用Framer的useAnimation
挂钩。为此,我添加了以下内容:
const iconAnimation = useAnimation()
useEffect(() => {
iconAnimation.start("fadeIn")
.then(() => {
iconAnimation.start("slideOut")
});
}, [iconAnimation])
我还更改了为此提供图标的代码:
const renderIcons = () =>
icons?.map(({src, alt}, index) => (
<motion.li
initial='hidden'
custom={index}
animate={iconAnimation}
variants={iconAnimationVariants}
className="intro-icon"
key={index}>
<img src={src} alt={alt} />
</motion.li>
))
通过这些更改,图标根本不会动画。当我在array.map
中使用时,似乎iconanimation
被完全忽略。我也有一个类似的设置,用于简单的文本动画,其中正常工作。
有没有人可以解释这一点,或者也许我朝正确的方向指出? 如果您需要其他信息,请让我知道。 提前致谢, 杰罗恩
I've been playing around with Framer for the last couple of days. Currently I'm trying to animate a group of 3 icons. The first animation is to fade them in from the bottom, each icon with slightly bigger delay then the one before. The second animation is to fade them out horizontally. I've setup my animation variants like the code below:
const iconAnimationVariants = {
fadeIn: i => ({
opacity: [0, 1],
y: [20, 0],
transition: {
delay: i * 0.1,
duration: 0.6
}
}),
slideRight: i => ({
opacity: 0,
x: 20,
transition: {
delay: 2 + (i * 0.1),
duration: 1.6
}
})
}
Next, I use the code below to add the icons to my app and animate them.
const renderIcons = () =>
icons?.map(({src, alt}, index) => (
<motion.li
initial='hidden'
custom={index}
animate='fadeIn'
exit='slideRight'
variants={iconAnimationVariants}
className="intro-icon"
key={index}>
<img src={src} alt={alt} />
</motion.li>
))
This works, at least the fadeIn
part of the variant works. To have a bit more control I would like to use the useAnimation
hook from Framer. For this I added the following:
const iconAnimation = useAnimation()
useEffect(() => {
iconAnimation.start("fadeIn")
.then(() => {
iconAnimation.start("slideOut")
});
}, [iconAnimation])
I also changed the code that renders the icons to this:
const renderIcons = () =>
icons?.map(({src, alt}, index) => (
<motion.li
initial='hidden'
custom={index}
animate={iconAnimation}
variants={iconAnimationVariants}
className="intro-icon"
key={index}>
<img src={src} alt={alt} />
</motion.li>
))
With these changes the icons do not animate at all. It seems like the iconAnimation
is completely ignored when I use it inside an Array.map
. I have a similar setup for a simple text animation where this works just fine.
Is there anyone that could explain this a bit or maybe point me in the right direction?
Please let me know if you need any other information.
Thanks in advance,
Jeroen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论