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>

Logo

前往低代码交流专区

更多推荐