当前位置:首页 > VUE

vue展开收起功能实现

2026-01-20 18:32:15VUE

Vue 展开收起功能实现

使用 v-show 或 v-if 控制显示隐藏

通过 Vue 的指令 v-showv-if 可以轻松实现展开收起功能。v-show 通过 CSS 的 display 属性控制元素显示隐藏,适合频繁切换的场景;v-if 会直接销毁和重建 DOM,适合不频繁切换的场景。

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <div v-show="isExpanded">
      这里是需要展开收起的内容
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

使用 transition 添加动画效果

通过 Vue 的 <transition> 组件可以为展开收起添加平滑的动画效果。

vue展开收起功能实现

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <transition name="fade">
      <div v-show="isExpanded">
        这里是需要展开收起的内容
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s, max-height 0.5s;
  max-height: 500px;
  overflow: hidden;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
  max-height: 0;
}
</style>

使用第三方库如 vue-collapse

对于更复杂的展开收起需求,可以使用第三方库如 vue-collapse。安装后可以直接使用其提供的组件。

vue展开收起功能实现

npm install vue-collapse --save
<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <vue-collapse v-model="isExpanded">
      <div>
        这里是需要展开收起的内容
      </div>
    </vue-collapse>
  </div>
</template>

<script>
import VueCollapse from 'vue-collapse'

export default {
  components: {
    VueCollapse
  },
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

动态控制内容高度

通过动态绑定样式,可以实现内容高度的平滑过渡。

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <div :style="{ height: isExpanded ? 'auto' : '0', overflow: 'hidden', transition: 'height 0.5s' }">
      这里是需要展开收起的内容
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

使用 CSS 的 max-height 实现动画

通过设置 max-height 可以实现更平滑的展开收起动画,避免内容高度不确定的问题。

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <div class="content" :class="{ 'expanded': isExpanded }">
      这里是需要展开收起的内容
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style>
.content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.5s ease;
}
.content.expanded {
  max-height: 500px;
}
</style>

以上方法可以根据具体需求选择适合的方式实现展开收起功能。

标签: 功能vue
分享给朋友:

相关文章

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.defineProperty…

vue功能实现

vue功能实现

Vue 功能实现指南 Vue.js 是一个渐进式 JavaScript 框架,广泛用于构建用户界面。以下是 Vue 功能的常见实现方法。 数据绑定 Vue 的核心功能之一是数据绑定,通过 v-mod…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API)…