关于Vue-transition leave-to离开动画不生效的问题

transition组件嵌套

显示动画的元素应包含在 transition 组件内部(包含定位元素以及动画元素本身)

上代码对比区别

需在另外的vue文件中引入

有离开过渡动画效果
<template>
<!-- 注意transition的嵌套结构 -->
<transition appear name="fade">
    <div v-if="isToastShow" class="_G11fgeBX">
        <div class="_2EVeKrJa">{{toastMsg}}</div>
    </div>
</transition>
</template>

<script>
export default {
    name: "MyToast",
    props: {
        toastMsg: {
            type: String,
            default: 'toastMsg',
        }
    },
    data() {
        return {
            timeout: null,
            isToastShow: true,
        }
    },
    mounted() {
        if (this.timeout) {
            clearTimeout(this.timeout)
        }
        this.timeout = setTimeout(() => {
            this.hideToast();
        }, 3000);
    },
    methods: {
        hideToast() {
            this.isToastShow = false;
        },
    },
}
</script>

<style scoped>
.fade-enter-active {
    transition: all .3s ease-out;
}

.fade-leave-active {
    transition: all .1s ease-in;
}

.fade-enter,
.fade-leave-to

/* .fade-leave-active below version 2.1.8 */
    {
    opacity: 0;
    transform: scale(.6);
}

._G11fgeBX {
    position: fixed;
    left: 0;
    top: 50%;
    z-index: 999;
    width: 100%;
    height: 0;
    display: flex;
    align-items: center;
    justify-content: center
}

._2EVeKrJa {
    min-width: 80px;
    max-width: 300px;
    padding: 0 20px;
    height: 36px;
    line-height: 36px;
    border-radius: 18px;
    background-color: #111;
    font-size: 14px;
    text-align: center;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    box-shadow: 0 0 1px 3px hsla(0, 0%, 100%, .2)
}
</style>

无离开过渡动画效果
<template>
<!-- 注意transition的嵌套结构 -->
<div v-if="isToastShow" class="_G11fgeBX">
    <transition appear name="fade">
        <div class="_2EVeKrJa">{{toastMsg}}</div>
    </transition>
</div>
</template>

<script>
export default {
    name: "MyToast",
    props: {
        toastMsg: {
            type: String,
            default: 'toastMsg',
        }
    },
    data() {
        return {
            timeout: null,
            isToastShow: true,
        }
    },
    mounted() {
        if (this.timeout) {
            clearTimeout(this.timeout)
        }
        this.timeout = setTimeout(() => {
            this.hideToast();
        }, 3000);
    },
    methods: {
        hideToast() {
            this.isToastShow = false;
        },
    },
}
</script>

<style scoped>
.fade-enter-active {
    transition: all .3s ease-out;
}

.fade-leave-active {
    transition: all .1s ease-in;
}

.fade-enter,
.fade-leave-to

/* .fade-leave-active below version 2.1.8 */
    {
    opacity: 0;
    transform: scale(.6);
}

._G11fgeBX {
    position: fixed;
    left: 0;
    top: 50%;
    z-index: 999;
    width: 100%;
    height: 0;
    display: flex;
    align-items: center;
    justify-content: center
}

._2EVeKrJa {
    min-width: 80px;
    max-width: 300px;
    padding: 0 20px;
    height: 36px;
    line-height: 36px;
    border-radius: 18px;
    background-color: #111;
    font-size: 14px;
    text-align: center;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    box-shadow: 0 0 1px 3px hsla(0, 0%, 100%, .2)
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vuetransition 组件可以用于在组件的出现、消失、更新等时刻添加动画效果。 首先,在 Vue 模板中,你需要将要添加动画效果的组件包裹在 `<transition>` 标签中。然后,在 `<transition>` 标签上,你可以使用以下属性来指定动画效果: - `name`:指定动画效果的名称,该名称对应于你在 CSS 中定义的动画样式。 - `enter-class`、`enter-active-class`、`enter-to-class`:指定组件进入时的动画样式,分别对应于 CSS 中的 `.enter-class`、`.enter-active-class`、`.enter-to-class`。 - `leave-class`、`leave-active-class`、`leave-to-class`:指定组件离开时的动画样式,分别对应于 CSS 中的 `.leave-class`、`.leave-active-class`、`.leave-to-class`。 下面是一个简单的例子,展示了如何使用 `<transition>` 组件来为组件添加淡入淡出的动画效果: ```html <template> <div> <button @click="show = !show">Toggle</button> <transition name="fade"> <p v-if="show">Hello, world!</p> </transition> </div> </template> <script> export default { data() { return { show: false } } } </script> <style> .fade-enter { opacity: 0; } .fade-enter-active { transition: opacity 0.5s; } .fade-enter-to { opacity: 1; } .fade-leave { opacity: 1; } .fade-leave-active { transition: opacity 0.5s; } .fade-leave-to { opacity: 0; } </style> ``` 在上面的例子中,我们定义了一个名为 `fade` 的动画效果,并在 CSS 样式中定义了 `fade-enter`、`fade-enter-active`、`fade-enter-to`、`fade-leave`、`fade-leave-active`、`fade-leave-to` 这些类,分别对应于组件进入时的动画、组件进入时动画生效时的样式、组件进入时动画结束后的样式、组件离开时的动画、组件离开动画生效时的样式、组件离开动画结束后的样式。 当我们在模板中点击 Toggle 按钮时,`show` 的值会切换,导致组件的显示状态发生变化,进而触发动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值