学习Vue3 第二十二章(transition-group过度列表)

  • 单个节点
  • 多个节点,每次只渲染一个

那么怎么同时渲染整个列表,比如使用 v-for?在这种场景下,我们会使用 <transition-group> 组件。在我们深入例子之前,先了解关于这个组件的几个特点:

  • 默认情况下,它不会渲染一个包裹元素,但是你可以通过 tag attribute 指定渲染一个元素。
  • 过渡模式不可用,因为我们不再相互切换特有的元素。
  • 内部元素总是需要提供唯一的 key attribute 值。
  • CSS 过渡的类将会应用在内部的元素中,而不是这个组/容器本身。
<transition-group>
     <div style="margin: 10px;" :key="item" v-for="item in list">{{ item }</div>
</transition-group>
const list = reactive<number[]>([1, 2, 4, 5, 6, 7, 8, 9])
const Push = () => {
    list.push(123)
}
const Pop = () => {
    list.pop()
}

2.列表的移动过渡

<transition-group> 组件还有一个特殊之处。除了进入和离开,它还可以为定位的改变添加动画。只需了解新增的 v-move 类就可以使用这个新功能,它会应用在元素改变定位的过程中。像之前的类名一样,它的前缀可以通过 name attribute 来自定义,也可以通过 move-class attribute 手动设置

下面代码很酷炫

<template>
    <div>
        <button @click="shuffle">Shuffle</button>
        <transition-group class="wraps" name="mmm" tag="ul">
            <li class="cell" v-for="item in items" :key="item.id">{{ item.number }}</li>
        </transition-group>
    </div>
</template>
  
<script setup  lang='ts'>
import _ from 'lodash'
import { ref } from 'vue'
let items = ref(Array.apply(null, { length: 81 } as number[]).map((_, index) => {
    return {
        id: index,
        number: (index % 9) + 1
    }
}))
const shuffle = () => {
    items.value = _.shuffle(items.value)
}
</script>
  
<style scoped lang="less">
.wraps {
    display: flex;
    flex-wrap: wrap;
    width: calc(25px * 10 + 9px);
    .cell {
        width: 25px;
        height: 25px;
        border: 1px solid #ccc;
        list-style-type: none;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}

.mmm-move {
    transition: transform 0.8s ease;
}
</style>

3.状态过渡

Vue 也同样可以给数字 Svg 背景颜色等添加过度动画 今天演示数字变化

<template>
    <div>
        <input step="20" v-model="num.current" type="number" />
        <div>{{ num.tweenedNumber.toFixed(0) }}</div>
    </div>
</template>
    
<script setup lang='ts'>
import { reactive, watch } from 'vue'
import gsap from 'gsap'
const num = reactive({
    tweenedNumber: 0,
    current:0
})

watch(()=>num.current, (newVal) => {
    gsap.to(num, {
        duration: 1,
        tweenedNumber: newVal
    })
})

</script>
    
<style>
</style>
Vue3的`<transition-group>`组件可以让我们在多个元素之间添加过渡动画。它与Vue2的`<transition-group>`使用方式类似,不过在Vue3中使用需要注意一些细节。 下面是一个简单的例子,展示了如何使用`<transition-group>`: ```html <template> <div> <button @click="addItem">Add Item</button> <transition-group name="list" tag="ul"> <li v-for="item in items" :key="item.id">{{ item.name }}</li> </transition-group> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const items = ref([{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }]); const addItem = () => { const id = items.value.length + 1; items.value.push({ id, name: `Item ${id}` }); }; return { items, addItem, }; }, }; </script> <style> .list-enter-active, .list-leave-active { transition: all 0.5s; } .list-enter, .list-leave-to { opacity: 0; transform: translateY(30px); } </style> ``` 在上面的例子中,我们使用了`<transition-group>`包裹了一个`<ul>`元素,然后使用`v-for`指令渲染了多个`<li>`元素。注意到每个`<li>`元素都有一个`key`属性,用来帮助Vue3识别哪些元素是已经存在的,哪些元素是新添加的或者被删除的。 我们还定义了一些CSS样式来定义动画效果。`.list-enter-active`和`.list-leave-active`样式分别用于在元素插入和删除时添加过渡效果,`.list-enter`和`.list-leave-to`样式则定义了具体的过渡效果。在这个例子中,我们定义了一个简单的淡入淡出和垂直平移的效果。 如果你想了解更多关于Vue3的`<transition-group>`组件的使用,请参考官方文档:[Transitions & Animations - Transitioning Between Elements](https://v3.vuejs.org/guide/transitions-overview.html#transitioning-between-elements)。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小满zs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值