transition-group、过渡
上节的transition动画组件适用于单一节点,多个节点时,每次只会渲染一个。
因此有v-for等多个节点需要渲染整个列表动画时,可使用<transition-group>组件。
<transition-group>组件特点:
- 默认不会再包裹一层元素,通过tag属性可用一个特定元素包裹。
- 过渡模式、单个
<transition>不可用 - CSS 过渡的类将会应用在内部的元素中,而不是这个组/容器本身
基本使用
<transition-group>
<div style="margin: 10px;" :key="item" v-for="item in list">{{ item }</div>
</transition-group>
列表的移动过渡
<transition-group> 支持通过 CSS 变换移动过渡。当孩子在屏幕上的位置在更新后发生变化时,它将应用一个移动的 CSS 类(从 name 属性自动生成或使用 move-class 属性配置)。如果在应用移动类时 CSS transform 属性为 "transition-able",则元素将使用 FLIP technique 平滑地动画到其目的地。
<template>
<TransitionGroup tag="ul" name="slide">
<li v-for="item in items" :key="item.id">
{{ item.text }}
</li>
</TransitionGroup>
</template>
<style lang="less" scoped>
.slide-move {
transition: transform 0.8s ease;
}
</style>
或者
<template>
<TransitionGroup tag="ul" move-class="slide">
<li v-for="item in items" :key="item.id">
{{ item.text }}
</li>
</TransitionGroup>
</template>
<style lang="less" scoped>
.slide {
transition: transform 0.8s ease;
}
</style>
状态过渡
利用gsap,给数字 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>
