vue3 transition-group详解

transition-group组件是一个非常有用的工具,可以在Vue应用中实现过渡效果。它可以将多个元素包含在一个动态列表中,并在元素之间进行过渡。上一章我们讲了 transition的基本用法,本章将会讲解transition-group,transition和animate组件库的配合使用还有关于平移过渡动画的案例

一、transition-group

<transition-group> 是一个内置组件,用于对 v-for 列表中的元素或组件的插入、移除和顺序改变添加动画效果

遵循步骤

  • 将需要过渡的元素放入transition-group组件内部。
  • 使用v-for指令遍历数据列表,并为每个元素设置唯一的key属性
  • 定义过渡效果的样式
<template>
  <button @click="addBox">增加盒子</button>
  <transition-group
    tag="div"
    class="box-wrap"
    enter-active-class="animate__animated animate__bounceIn"
    leave-active-class="animate__animated animate__bounceOut"
  >
    <div
      v-for="item in boxList"
      :key="item.id"
      :style="{ backgroundColor: item.bgColor }"
      class="box"
    ></div>
  </transition-group>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";

const boxList = reactive([
  {
    id: 1,
    bgColor: "#666666",
  },
]);
const addBox = () => {
  boxList.push({
    id: boxList.length + 1,
    bgColor: color16(),
  });
};
const color16 = () => {
  const r = Math.floor(Math.random() * 256);
  const g = Math.floor(Math.random() * 256);
  const b = Math.floor(Math.random() * 256);
  const color = `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
  return color;
};
</script>
<style scoped lang="scss">
.box-wrap {
  display: flex;
  align-item: center;
  justify-content: space-between;
}
.box {
  width: 200px;
  height: 200px;
  margin: 10px;
}
</style>

在上面案例中,首先需要安装animate.css组件库
npm install animate.css --save
其中tag指定一个元素作为容器元素来渲染,其余用法和animate一致。需要注意的是,使用animate.css组件库类名时前面需要加上animate__animated才会生效。

效果图

二、平移过渡动画

<template>
  <button class="btn" @click="changeNumber">改变顺序</button>
  <transition-group tag="div" class="box-wrap" name="list">
    <div class="box" v-for="item in numList" :key="item.id">
      {{ item.num }}
    </div>
  </transition-group>
</template>

<script setup lang="ts">
import { ref, reactive, toRef } from "vue";
import _ from "lodash";
const nums = new Array(81).fill(undefined).map((item, index) => {
  return {
    id: index,
    num: (index % 9) + 1,
  };
});
let numList = toRef(nums);

const changeNumber = () => {
  numList.value = _.shuffle(numList.value);
};
</script>
<style scoped lang="scss">
.btn {
  margin-bottom: 16px;
}
.box-wrap {
  display: flex;
  flex-wrap: wrap;
  width: calc(30px * 9 + 10px);
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
}
.box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
}
.list-move {
  /* 对移动中的元素应用的过渡 */
  transition: all 1s;
}
</style>

上面代码中用到lodash的shuffle方法实现打乱集合效果
npm i --save lodash

效果图:

三、总结

transition和animate组件库结合为Vue应用添加出色的过渡效果,提升用户体验。无论是创建任务列表、图片轮播还是其他场景,transition-group都是一个必备的工具。

`vue-transition-group`是Vue.js的官方动画库,它提供了一组基础的CSS类用于实现元素的进出和切换动画。以下是使用`vue-transition-group`的一个简单示例: 首先,在你的Vue组件中安装`vue-transition-group`,如果还没有安装,可以在`main.js`或其他适当的地方添加: ```bash npm install vue@next vue-transition-group # 或者 yarn add vue@next vue-transition-group ``` 接着,将`TransitionGroup`组件导入到你的组件文件中,并在需要动画的元素上使用`v-enter`, `v-enter-active`, `v-leave-to`, 等修饰符: ```html <template> <div id="app"> <transition-group tag="ul" name="list-animation"> <li v-for="(item, index) in items" :key="index"> {{ item }} <button @click="removeItem(index)">Remove</button> </li> </transition-group> </div> </template> <script> import { transitionGroup } from 'vue-transition-group'; export default { components: { TransitionGroup, }, data() { return { items: ['Item 1', 'Item 2', 'Item 3'], }; }, methods: { removeItem(index) { this.items.splice(index, 1); }, }, }; </script> <style scoped> .list-animation-enter-active, .list-animation-leave-active { transition: opacity 0.5s; } .list-animation-enter, .list-animation-leave-to { opacity: 0; } </style> ``` 在这个例子中,当点击删除按钮时,对应的列表项会淡入(enter)或淡出(leave),并且在删除过程中有一个过渡动画。你可以调整`.list-animation-enter-active` 和 `.list-animation-leave-active` 中的样式属性来自定义动画速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值