当前位置:首页 > VUE

vue实现展开与收起

2026-03-27 02:21:25VUE

Vue 实现展开与收起功能

使用 v-show 指令

通过 v-show 控制元素的显示与隐藏,结合按钮切换状态:

<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>

使用 v-if 指令

v-show 类似,但 v-if 会完全销毁和重建 DOM 元素:

vue实现展开与收起

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

动态高度过渡动画

结合 CSS 过渡实现平滑动画效果:

vue实现展开与收起

<template>
  <div>
    <button @click="toggle">{{ isExpanded ? '收起' : '展开' }}</button>
    <div class="content" :style="{ height: isExpanded ? 'auto' : '0' }">
      <div class="inner">需要展开/收起的内容</div>
    </div>
  </div>
</template>

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

<style>
.content {
  overflow: hidden;
  transition: height 0.3s ease;
}
.inner {
  padding: 10px;
}
</style>

使用第三方组件

vue-collapse 等现成组件:

<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>
    <div v-for="item in items" :key="item.id">
      <button @click="toggle(item)">{{ item.expanded ? '收起' : '展开' }}</button>
      <div v-show="item.expanded">{{ item.content }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, content: '内容1', expanded: false },
        { id: 2, content: '内容2', expanded: false }
      ]
    }
  },
  methods: {
    toggle(item) {
      item.expanded = !item.expanded
    }
  }
}
</script>

注意事项

  • v-show 只是切换 display 属性,适合频繁切换的场景
  • v-if 会完全销毁和重建 DOM,适合不频繁切换的场景
  • 过渡动画需要配合 CSS 的 transition 属性实现
  • 动态高度计算时可能需要获取元素的实际高度
  • 多级展开时注意数据结构的合理设计

标签: vue
分享给朋友:

相关文章

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue实现多选

vue实现多选

Vue 实现多选功能 在 Vue 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组(…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…