【Vue3】 第十六部分 transition

本文详细介绍了Vue3中transition组件的使用方法,包括基本用法、配合animate.css使用、Appear属性的应用、transitionGroup的多种场景实践等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【Vue3】 第十六部分 transition



16. transition

16.1 transition的基本使用(name属性)

在这里插入图片描述

<script lang="ts" setup>
    import {ref} from 'vue'
    const flag = ref<boolean>(true)
</script>

<template>
    <div>
        <button @click="flag = !flag">切换</button>
        <transition name="fade">
            <h1 v-if="flag">大家好!</h1>
        </transition>
    </div>
</template>

<style scoped lang='less'>
    // 根据所取的名字,会提供6个类
    .fade-enter-from{
        // 进入时的过渡前
        opacity: 0;
    }
    .fade-enter-active{
        // 进入时的过渡中
        transition: all 1s ease;
    }
    .fade-enter-to{
        // 进入时的过渡结束
        opacity: 1;
    }
    .fade-leave-from{
        // 离开时的过渡前
        opacity: 1;
    }
    .fade-leave-active{
        // 离开时的过渡中
        transition: all 1s ease;
    }
    .fade-leave-to{
        // 离开时的过渡结束
        opacity: 0;
    }
</style>

16.2 transition+animate.css使用

在这里插入图片描述

安装animate.css

$ npm install animate.css --save
$ yarn add animate.css
<script lang="ts" setup>
    import {ref} from 'vue'
		// 引入样式
    import 'animate.css';
    const flag = ref<boolean>(true)
</script>

<template>
    <div>
        <button @click="flag = !flag">切换</button>
        <!-- 直接使用这两个属性,因为开头和结尾都只有一帧而已 -->
        <transition enter-active-class="animate__animated animate__bounceInDown" leave-active-class="animate__animated animate__fadeOutDown">
            <h1 v-if="flag">大家好!</h1>
        </transition>
    </div>
</template>

16.3 transition中Appear属性

这个属性可以在页面加载完后立即执行对应的状态,也就是立即执行动画过渡效果

<script lang="ts" setup>
    import {ref} from 'vue'
    import 'animate.css';
    const flag = ref<boolean>(true)
</script>

<template>
    <div>
        <!-- 页面加载完后立即执行-->
        <transition appear appear-active-class="animate__animated animate__bounceInDown">
            <h1 v-if="flag">大家好!</h1>
        </transition>
    </div>
</template>

16.4 transitionGroup

16.4.1 配合animate使用

在这里插入图片描述

用于同时渲染整个列表,例如:使用v-for的时候

<script lang="ts" setup>
    import {ref} from 'vue'
    import "animate.css"
    const list = ref<number[]>([1,2,3])
    const add = () =>{
        list.value.push(list.value.length + 1)
    }
    const del = () =>{
        list.value.pop()
    }
</script>

<template>
    <div>
        <div class="wrapper">
            <transition-group enter-active-class="animate__animated animate__lightSpeedInRight" leave-active-class="animate__animated animate__rotateOutDownLeft">
                <div class="item" v-for="item in list" :key="item">{{item}}</div>
            </transition-group>
        </div>
        <button @click="add">add</button>
        <button @click="del">del</button>
    </div>
</template>

<style scoped lang='less'>
    .wrapper{
        display: flex;
        .item{
            margin: 5px;
        }
    }
</style>

16.4.2 平移过渡动画

在这里插入图片描述

$ npm i -g npm
$ npm i --save lodash
<script lang="ts" setup>
import _ from 'lodash';
import { ref } from 'vue';
let list = ref(Array.apply(null, { length: 81 } as number[]).map((_, index) => {
    return {
        id: index,
        num: index % 9 + 1
    }
}))
const handleRandom = () =>{
    list.value = _.shuffle(list.value)
}
</script>

<template>
    <div>
        <button @click="handleRandom">随机排序</button>
        <div class="wrapper">
            <!-- move-class:平移变化 -->
            <transition-group move-class="move">
                <div class="item" v-for="item in list" :key="item.id">{{item.num}}</div>
            </transition-group>
        </div>
    </div>
</template>

<style scoped lang='less'>
    .wrapper{
        display: flex;
        flex-wrap: wrap;
        width: calc(40px*9 + 9px);
        border-left:1px solid black ;
        border-top:1px solid black ;
        .item{
            border: 1px solid black;
            padding: 5px;
            width: 30px;
            line-height: 30px;
            text-align: center;
            border-left: none;
            border-top: none ;
        }
    }
    .move{
        transition: all 1s;
    }
</style>

16.4.3 状态过渡

在这里插入图片描述

npm install gsap
<script lang="ts" setup>
    import {reactive, ref, watch} from 'vue'
    import gsap from "gsap";
    import "animate.css"
    const num = reactive({
        current:0,
        newVal:0
    })

    // 监视当前的值是否发生变化
    watch(()=>num.current,(newVal,oldVal) =>{
        // 使用这个gasp去过渡,它可以接收一个对象
        gsap.to(num,{
            newVal:newVal,
            // 持续时间
            duration:3,
            // 过渡曲线
            ease:"espo.out"
        })
    })
</script>

<template>
    <div>
        <div>
            <input type="number" v-model="num.current" step="30">
            <transition-group>
                <h2>{{num.newVal.toFixed()}}</h2>
            </transition-group>
        </div>
    </div>
</template>

<style scoped lang='less'>
</style>

总结

以上就是今天要讲的内容,希望对大家有所帮助!!!

### Vue3 Transition 动态高度解决方案 在Vue3中实现`<transition>`标签下的动态高度变化,主要挑战在于CSS `height`属性由`auto`到特定值的变化无法触发平滑过渡。为了使动画正常工作,需要确保`height`始终具有具体的像素值。 一种有效的方法是在元素显示时计算其自然高度,并将其应用为内联样式[^1]: ```html <div id="app"> <button @click="toggle">Toggle</button> <transition @before-enter="beforeEnter" @enter="enter" @leave="leave"> <div v-if="visible" ref="content"></div> </transition> </div> ``` ```javascript const app = { data() { return { visible: true, }; }, methods: { toggle() { this.visible = !this.visible; }, beforeEnter(el) { el.style.height = &#39;0&#39;; }, enter(el, done) { let height = `${el.scrollHeight}px`; el.style.height = height; el.addEventListener(&#39;transitionend&#39;, done); }, leave(el, done) { const height = `${el.scrollHeight}px`; el.style.height = height; // 设置当前高度 setTimeout(() => (el.style.height = &#39;0&#39;), 16); // 下一帧设为0 el.addEventListener(&#39;transitionend&#39;, done); } } }; createApp(app).mount(&#39;#app&#39;); ``` 此方法利用JavaScript钩子函数,在进入和离开阶段分别控制目标元素的高度,从而实现了基于内容的实际尺寸来调整容器大小的效果[^3]。 对于更复杂的场景,还可以考虑使用第三方库如Velocity.jsGSAP简化这些操作;另外也可以探索其他替代方案比如使用`max-height`配合较大的固定值模拟展开收缩效果,但这可能会影响性能并且不够精确[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值