当前位置:首页 > VUE

vue 实现展开动画

2026-02-21 03:54:11VUE

Vue 实现展开动画的方法

使用 Vue Transition 组件

Vue 内置的 <transition> 组件可以方便地实现展开/折叠动画效果。通过设置 CSS 过渡或动画类名,控制元素高度的变化。

vue 实现展开动画

<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <transition name="expand">
      <div v-show="isExpanded" class="content">
        Content to be expanded
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  }
}
</script>

<style>
.expand-enter-active, .expand-leave-active {
  transition: height 0.3s ease;
  overflow: hidden;
}
.expand-enter, .expand-leave-to {
  height: 0 !important;
}
</style>

动态计算高度

如果内容高度不确定,可以通过 JavaScript 动态获取高度并设置过渡效果。

vue 实现展开动画

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div class="container" :style="{ height: currentHeight + 'px' }">
      <div ref="content" class="content">
        Content with dynamic height
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      currentHeight: 0
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
      this.currentHeight = this.isExpanded ? this.$refs.content.scrollHeight : 0
    }
  }
}
</script>

<style>
.container {
  transition: height 0.3s ease;
  overflow: hidden;
}
</style>

使用第三方动画库

可以结合第三方动画库如 Animate.css 或 GSAP 实现更复杂的展开动画效果。

<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <transition
      enter-active-class="animate__animated animate__fadeInDown"
      leave-active-class="animate__animated animate__fadeOutUp"
    >
      <div v-show="isExpanded" class="content">
        Content with Animate.css
      </div>
    </transition>
  </div>
</template>

<script>
import 'animate.css'
export default {
  data() {
    return {
      isExpanded: false
    }
  }
}
</script>

使用 Vue 插件

可以安装专门的 Vue 过渡插件如 vue-slide-up-down 或 vue-collapse 来简化实现。

npm install vue-slide-up-down
<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <slide-up-down v-model="isExpanded" :duration="300">
      Content with vue-slide-up-down
    </slide-up-down>
  </div>
</template>

<script>
import SlideUpDown from 'vue-slide-up-down'
export default {
  components: { SlideUpDown },
  data() {
    return {
      isExpanded: false
    }
  }
}
</script>

每种方法都有其适用场景,可以根据项目需求选择最合适的实现方式。简单的展开动画使用 Vue Transition 组件即可,复杂动画可以考虑第三方库或插件。

标签: 动画vue
分享给朋友:

相关文章

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue实现单据

vue实现单据

Vue 实现单据功能的方法 使用 Vue 实现单据功能通常涉及表单设计、数据绑定、验证和提交等环节。以下是具体实现方案: 表单设计与数据绑定 使用 Vue 的 v-model 指令实现表单数据的双向…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来定义…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…