当前位置:首页 > 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 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…

vue实现公告

vue实现公告

Vue 实现公告功能的方法 公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式: 使用 marquee 标签实现滚动公告 <template> <…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现尺子

vue实现尺子

Vue 实现尺子组件 实现一个尺子组件可以通过 Canvas 或纯 CSS 完成。以下是两种常见实现方式: 使用 Canvas 绘制尺子 通过 Canvas 动态绘制刻度线和数字,适合需要高精度或动…

vue实现tip

vue实现tip

Vue实现Tooltip的方法 使用Vue实现Tooltip可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML的title属性 在Vue模板中直接使用HTML的title属性是最简单的实…