You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Framer Motion中Exit过渡失效问题求助

解决Framer Motion AnimatePresence Exit过渡不生效问题

问题根源

你代码里把transition属性嵌套在了animate对象内部,不符合Framer Motion的API规范,导致Exit过渡逻辑无法被正确识别。另外需确保isOpen状态切换时,motion组件能从DOM中真正移除,而非仅通过样式隐藏。

修正后的代码

import { motion, AnimatePresence } from 'framer-motion';

<AnimatePresence>
  {isOpen && (
    <motion.div
      key="modal"
      ref={sheetRef}
      initial={{ y: '100%' }}
      animate={{ y: '0%' }}
      exit={{ y: '100%' }}
      transition={{ duration: 0.2, ease: 'linear' }}
    >
      ......
    </motion.div>
  )}
</AnimatePresence>

额外验证点

  • 确认isOpen状态切换时,motion.div确实从DOM结构中被移除
  • 保证motion组件的key属性唯一且稳定,避免状态切换时key变化导致组件重新挂载而非触发移除逻辑

内容的提问来源于stack exchange,提问作者Vishal Tanna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 00:55:54