当前位置:首页 > 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实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript…

vue 指令实现

vue 指令实现

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

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现水印

vue实现水印

Vue 实现水印的方法 使用 CSS 背景图 通过 CSS 的 background-image 和 background-repeat 属性实现水印效果。创建一个包含水印文本的 Canvas,将其转…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transit…