在array.map中使用Framer的UseAnimation钩

发布于 2025-01-28 02:08:21 字数 1899 浏览 3 评论 0原文

在过去的几天里,我一直在玩框架。目前,我正在尝试为一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文