当前位置:首页 > VUE

vue实现展开收拢

2026-01-18 22:43:50VUE

Vue 实现展开/收拢功能

在 Vue 中实现展开和收拢功能可以通过多种方式实现,以下是一些常见的方法:

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

通过绑定一个布尔值来控制元素的显示和隐藏:

<template>
  <div>
    <button @click="toggle">展开/收拢</button>
    <div v-show="isExpanded">
      这是要展开/收拢的内容
    </div>
  </div>
</template>

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

使用 CSS 过渡效果

结合 Vue 的过渡系统实现平滑的展开/收拢动画:

<template>
  <div>
    <button @click="toggle">展开/收拢</button>
    <transition name="slide">
      <div v-show="isExpanded" class="content">
        这是带有过渡效果的内容
      </div>
    </transition>
  </div>
</template>

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

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.5s ease;
  max-height: 500px;
  overflow: hidden;
}
.slide-enter, .slide-leave-to {
  max-height: 0;
}
.content {
  background: #f5f5f5;
  padding: 10px;
}
</style>

使用第三方组件库

许多 Vue UI 组件库提供了现成的展开/收拢组件:

<template>
  <el-collapse v-model="activeNames">
    <el-collapse-item title="可折叠内容" name="1">
      <div>这是折叠内容</div>
    </el-collapse-item>
  </el-collapse>
</template>

<script>
export default {
  data() {
    return {
      activeNames: ['1']
    }
  }
}
</script>

动态高度过渡

对于高度不确定的内容,可以使用动态计算高度的方式:

<template>
  <div>
    <button @click="toggle">展开/收拢</button>
    <div ref="content" class="content" :style="{ height: contentHeight }">
      <div ref="inner">这是动态高度的内容...</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      contentHeight: '0px'
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
      if (this.isExpanded) {
        this.contentHeight = `${this.$refs.inner.offsetHeight}px`
      } else {
        this.contentHeight = '0px'
      }
    }
  }
}
</script>

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

多级展开/收拢

对于多级展开收拢的场景,可以使用递归组件:

vue实现展开收拢

<template>
  <div>
    <div v-for="item in items" :key="item.id">
      <div @click="toggle(item)">
        {{ item.title }}
        <span>{{ item.expanded ? '-' : '+' }}</span>
      </div>
      <div v-show="item.expanded">
        {{ item.content }}
        <tree-item v-if="item.children" :items="item.children"></tree-item>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TreeItem',
  props: ['items'],
  methods: {
    toggle(item) {
      item.expanded = !item.expanded
    }
  }
}
</script>

以上方法可以根据具体需求选择使用,简单的显示/隐藏使用 v-show 或 v-if 即可,需要动画效果则使用过渡系统,复杂场景可以考虑使用组件库或自定义递归组件。

标签: vue
分享给朋友:

相关文章

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Objec…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue 实现聊天

vue 实现聊天

使用 Vue 实现聊天功能 创建 Vue 项目并安装依赖 确保已安装 Vue CLI,通过以下命令创建新项目: vue create chat-app 进入项目目录后,安装必要的依赖(如 Socke…

vue实现折叠

vue实现折叠

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

vue实现单击

vue实现单击

Vue 实现单击事件的方法 在 Vue 中实现单击事件可以通过 v-on 指令或简写的 @ 符号绑定事件处理器。以下是几种常见的实现方式: 基础用法 通过 v-on:click 或 @click 绑…

mui实现vue

mui实现vue

mui 实现 Vue 的步骤 mui 是一个轻量级的前端框架,常用于移动端开发。结合 Vue 可以实现高效的开发体验。以下是具体实现方法。 安装 mui 和 Vue 通过 npm 或 yarn 安装…