简体   繁体   English

Framer Motion,我们可以只在进入时交错项目,但同时退出它们吗?

[英]Framer Motion , can we stagger items only on enter, but simultaneously exit them?

We're staggering animations using Framer Motion with some code like below.我们使用 Framer Motion 和下面的一些代码来制作交错动画。 The problem is, we only want to stagger items in, not out.问题是,我们只想错开项目,而不是错开。 Is there any way to specify staggering behavior specific to initial and exit , as opposed to the top level transition property we're using to define staggering now?有什么方法可以指定特定于initialexit的交错行为,而不是我们现在用来定义交错的顶级transition属性?

I think we could achieve this specifying "variants" but is it possible without adding that extra code and complexity?认为我们可以实现这个指定的“变体”,但是否有可能不添加额外的代码和复杂性?

<AnimatePresence exitBeforeEnter>
  {items.map((item, i) => (
     <motion.div
       initial={{ opacity: 0 }}
       animate={{ opacity: 1 }}
       exit={{ opacity: 0 }}
       transition={{ duration: 1, delay: i * 1 }}>
       {item}
     </motion.div>
   ))}
</AnimatePresence>

exitBeforeEnter has been deprecated for mode="wait" in v7.2在 v7.2 中已弃用 mode="wait" 的 exitBeforeEnter

<AnimatePresence mode="wait">
  {items.map((item, i) => (
     <motion.div
       key={item.id}
       initial={{ opacity: 0 }}
       animate={{ opacity: 1 }}
       exit={{ opacity: 0, transition: { duration: 0.5 } }}
       transition={{ duration: 1, delay: i * 1 }}>
       {item}
     </motion.div>
   ))}
</AnimatePresence>

You can specify a different transition for the exit prop:您可以为exit道具指定不同的过渡:

<AnimatePresence exitBeforeEnter>
  {items.map((item, i) => (
     <motion.div
       key={item.id} // don't forget the key!!!
       initial={{ opacity: 0 }}
       animate={{ opacity: 1 }}
       exit={{ opacity: 0, transition: { duration: 0.5 } }}
       transition={{ duration: 1, delay: i * 1 }}>
       {item}
     </motion.div>
   ))}
</AnimatePresence>

Your other animations will continue to use the default transition from the transition prop.您的其他动画将继续使用来自transition道具的默认过渡。

Also, don't forget to add a unique key to each element , or your exit animations won't work.另外,不要忘记为每个元素添加一个唯一的key ,否则您的退出动画将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM