简体   繁体   English

Framer Motion Animate Presence 退出问题

[英]Framer motion Animate Presence exit issue

I am having an issue with framer-motion AnimatePresence component.我遇到了 framer-motion AnimatePresence 组件的问题。 I am trying to start the animation after the component is visible within the viewport, to achieve this, I used react-intersection-observer.我试图在组件在视口中可见后开始动画,为了实现这一点,我使用了 react-intersection-observer。 It works as intended for the starting animation, but the exit animation breaks and I am not sure what is causing the problem.它按预期用于开始动画,但退出动画中断,我不确定是什么导致了问题。 I created a sandbox that reproduces this behavior at https://codesandbox.io/s/holy-dream-rb5gu?file=/src/index.js我在https://codesandbox.io/s/holy-dream-rb5gu?file=/src/index.js创建了一个沙箱来重现这种行为

It looks like using the imperativeAnimationControls API is overriding the other declarative animation settings for the element (like exit ).看起来使用命令式AnimationControls API会覆盖元素的其他声明性动画设置(如exit )。

Changing that animate prop to just accept a variant instead of an animation control seems to work:将该animate道具更改为仅接受变体而不是动画控件似乎可行:
Code Sandbox example 代码沙盒示例

I added a state for the current variant.我为当前变体添加了一个状态。 Starts as hidden and updates to visible when inView changes:inView更改时,以hidden开始并更新为visible

// use variant instead of Animation Controls
const [variant, setVariant] = useState("hidden");

useEffect(() => {
  if (inView) {
    setVariant("visible");
  }
}, [inView]);

Then use that variant state to update the animation prop on the div:然后使用该变体状态更新 div 上的动画道具:

<AnimatePresence exitBeforeEnter>
  {show && (
    <motion.div
      ref={ref}
      variants={containerVariants}
      initial="hidden"
      animate={variant} // use variant instead of Animation Controls
      exit="hidden"
    >

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

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