Vue3的内置组件 -实现过渡动画 TransitionGroup

Vue3的内置组件 -实现过渡动画 TransitionGroup

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

支持和 基本相同的 props、CSS 过渡 class 和 JavaScript 钩子监听器,但有以下几点区别:

  • 默认情况下,它不会渲染一个容器元素。但你可以通过传入 tag prop 来指定一个元素作为容器元素来渲染。

  • 过渡模式在这里不可用,因为我们不再是在互斥的元素之间进行切换。

  • 列表中的每个元素都必须有一个独一无二的 key attribute。

  • CSS 过渡 class 会被应用在列表内的元素上,而不是容器元素上

基本用法
复制代码
<template>
   <button @click="addItem">增加item</button>
  <button @click="removeItem">移除item</button>
  <TransitionGroup name="list" tag="div">
    <div v-for="(item, index) in items" :key="item" :data-index="index">
      {{ item }}
    </div>
  </TransitionGroup>
</template>

<script setup lang="ts">
 const items = ref(['Item 1', 'Item 2', 'Item 3']);
    const nextId = ref(4);

    const addItem = () => {
      items.value.push(`Item ${nextId.value++}`);
    };

    const removeItem = () => {
      items.value.pop();
    };
</script>

<style scoped>
.list-enter-active,
.list-leave-active {
  transition: all 0.5s;
}
.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
</style>
移动动画

上面的示例有一些明显的缺陷:当某一项被插入或移除时,它周围的元素会立即发生"跳跃"而不是平稳地移动。我们可以通过添加一些额外的 CSS 规则来解决这个问题

复制代码
<template>
   <button @click="addItem">在任意位置添加一项</button>
   <button @click="removeItem">移除任意位置上的一项</button>
   <button @click="shuffleItems">排序位置</button>
   <TransitionGroup name="list" tag="div">
    <div v-for="(item, index) in items" :key="item" :data-index="index">
      {{ item }}
    </div>
   </TransitionGroup>
</template>

<script setup lang="ts">
 const items = ref(['Item 1', 'Item 2', 'Item 3']);
    const nextId = ref(4);

    const addItem = () => {
      items.value.push(`Item ${nextId.value++}`);
    };

    const removeItem = () => {
      items.value.pop();
    };
    const shuffleItems = () => {
      items.value = items.value.sort(() => Math.random() - 0.5);
    };
</script>

<style scoped>
.list-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.list-item {
  padding: 10px;
  background-color: #f0f0f0;
  border-radius: 4px;
}
.list-enter-active,
.list-leave-active,
.list-move { /* 对移动中的元素应用的过渡 */
  transition: all 0.5s;
}
.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
/* 确保将离开的元素从布局流中删除
  以便能够正确地计算移动的动画。 */
.list-leave-active {
  position: absolute;
}
</style>
自定义过渡组 class

你还可以通过向 传递 moveClass prop 为移动元素指定自定义过渡 class,类似于自定义过渡 class。

在 中,你可以通过以下属性来自定义过渡类:

  • enter-active-class:定义进入时的活动类。

  • leave-active-class:定义离开时的活动类。

  • move-class:定义移动时的类(用于列表项的重新排序动画)。
    这些类允许你自定义进入、离开和移动时的动画效果。

渐进延迟列表动画
  • 安装gsap库:

    npm install gsap --save

  • 在组件中中引入gsap库:

    import gsap from 'gsap';

  • 在组件中使用gsap库:

相关推荐
神奇的程序员18 小时前
我的软件冲进苹果商店下载榜前 50 了
前端
阳光是sunny18 小时前
别再被 worktree 绕晕了!AI 编程时代你必须掌握的 Git 隔离神器
前端·人工智能·后端
万少20 小时前
万少的博客 - 技术分享与解决方案
前端·javascript·后端
尘世中一位迷途小书童1 天前
用 Cesium 撸了一个森林火情监控大屏,弧线、粒子、发光效果都齐了
前端·javascript
IT_陈寒1 天前
垃圾回收器选错了,我的Java服务内存炸了
前端·人工智能·后端
月光下的丝瓜1 天前
Flutter 国内安装指南
前端·flutter
先吃饱再说1 天前
JavaScript中`this` 的“千层套路”:从默认绑定到箭头函数的五种指向
javascript
玄星啊1 天前
AI 编程的第 30 天,我怀念古法 Coding 了
前端·ai编程
Jolyne_1 天前
Angular基础速通
前端·angular.js
foxire1 天前
基于nodejs实现服务端内核引擎
javascript