当前位置:首页 > 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 实现多层评论回复 数据结构设计 多层评论通常采用嵌套结构,每个评论对象包含子评论数组。示例数据结构如下: comments: [ { id: 1, content: '父…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…