You need to enable JavaScript to run this app. Vue3嵌套transition组件实现登录弹窗动画时离开类未生效问题
优惠活动
大模型
产品
解决方案
定价
更多

Vue3嵌套transition组件实现登录弹窗动画时离开类未生效问题

问题原因

内层<transition>的直接子元素没有绑定v-if条件,只有进入阶段因为设置了appear属性才会触发初始动画;离开时外层背景div的v-if先变为false,内层transition没有检测到自身子元素的显示状态变化,还没来得及触发离开流程就被外层元素连带销毁,所以不会添加离开类。

解决方法

方法1:调整嵌套transition的触发条件(保留现有嵌套结构)

给内层transition的直接子元素也绑定同一个v-if="showLogin",让两个transition同时检测到显示状态变化,同步触发进入/离开动画,原有CSS无需修改:

<template>
<transition name="popup">
    <div v-if="showLogin" class="absolute top-0 right-0 bottom-0 left-0 bg-black bg-opacity-50 flex flex-row flex-nowrap justify-center items-center select-none" @click="toggleLoginPopup(false)">
        <transition name="popup-inside">
            <!-- 给内层元素添加v-if绑定同一个控制变量 -->
            <div v-if="showLogin" class="shadow-lg bg-white rounded-sm p-4 border border-gray-300" @click.stop="">
                <div class="text-xl pb-4 text-center">
                    Sign In
                </div>
                <label class="block text-lg">
                    Username
                </label>
                <input type="text" class="w-full h-8 p-2 border border-black shadow-inner"/>
                <label class="block text-lg pt-4">
                    Password
                </label>
                <input type="password" class="w-full h-8 p-2 border border-black shadow-inner"/>
                <a class="block hover:cursor-pointer bg-purple-600 hover:bg-purple-800 text-xl w-full text-center rounded-sm text-white p-2 mt-4">
                    Login
                </a>
            </div>
        </transition>
    </div>
</transition>
</template>

方法2:简化结构,无需嵌套transition(更推荐)

直接通过后代选择器把两个动画效果绑定到外层的transition上,减少嵌套逻辑,也不会出现时序问题:

<template>
<transition name="popup">
    <div v-if="showLogin" class="absolute top-0 right-0 bottom-0 left-0 bg-black bg-opacity-50 flex flex-row flex-nowrap justify-center items-center select-none" @click="toggleLoginPopup(false)">
        <!-- 去掉内层transition,给表单加自定义类名 -->
        <div class="form-box shadow-lg bg-white rounded-sm p-4 border border-gray-300" @click.stop="">
            <div class="text-xl pb-4 text-center">
                Sign In
            </div>
            <label class="block text-lg">
                Username
            </label>
            <input type="text" class="w-full h-8 p-2 border border-black shadow-inner"/>
            <label class="block text-lg pt-4">
                Password
            </label>
            <input type="password" class="w-full h-8 p-2 border border-black shadow-inner"/>
            <a class="block hover:cursor-pointer bg-purple-600 hover:bg-purple-800 text-xl w-full text-center rounded-sm text-white p-2 mt-4">
                Login
            </a>
        </div>
    </div>
</transition>
</template>

对应CSS调整为:

<style scoped>
.popup-enter-active, .popup-leave-active {
    transition: all 5s ease;
}
/* 背景透明度动画 */
.popup-enter-from, .popup-leave-to {
    opacity: 0;
}
/* 表单平移动画,通过后代选择器绑定 */
.popup-enter-active .form-box, .popup-leave-active .form-box {
    transition: all 5s ease;
}
.popup-enter-from .form-box, .popup-leave-to .form-box {
    transform: translateY(-100px);
}
</style>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 06:06:03