当前位置:首页 > VUE

vue实现折叠组件

2026-01-08 08:28:08VUE

Vue 折叠组件实现方法

基于 v-show 和事件触发

通过 v-show 控制内容的显示与隐藏,结合点击事件切换状态。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div v-show="isOpen">
      <slot></slot>
    </div>
  </div>
</template>

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

添加过渡动画效果

使用 Vue 内置的 <transition> 组件实现平滑的展开/折叠动画。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <transition name="fade">
      <div v-show="isOpen">
        <slot></slot>
      </div>
    </transition>
  </div>
</template>

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

可复用组件封装

创建可配置的折叠面板组件,支持自定义标题和默认状态。

<template>
  <div class="collapse-panel">
    <div class="panel-header" @click="toggle">
      {{ title }}
      <span class="icon">{{ isOpen ? '-' : '+' }}</span>
    </div>
    <transition name="slide">
      <div class="panel-content" v-show="isOpen">
        <slot></slot>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: 'Collapse Panel'
    },
    defaultOpen: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isOpen: this.defaultOpen
    }
  },
  methods: {
    toggle() {
      this.isOpen = !this.isOpen
    }
  }
}
</script>

<style>
.panel-header {
  cursor: pointer;
  padding: 10px;
  background: #f5f5f5;
  border: 1px solid #ddd;
}
.panel-content {
  padding: 15px;
  border: 1px solid #ddd;
  border-top: none;
}
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.3s ease;
  max-height: 500px;
  overflow: hidden;
}
.slide-enter, .slide-leave-to {
  max-height: 0;
}
</style>

使用示例

<template>
  <div>
    <collapse-panel title="Section 1">
      Content for section 1
    </collapse-panel>
    <collapse-panel title="Section 2" :defaultOpen="true">
      Content for section 2
    </collapse-panel>
  </div>
</template>

<script>
import CollapsePanel from './CollapsePanel.vue'

export default {
  components: {
    CollapsePanel
  }
}
</script>

注意事项

  • 动态内容高度处理需要考虑 max-height 的合理设置
  • 多个折叠面板互斥展开可通过父组件管理状态实现
  • 移动端触摸事件可添加 @touchstart 支持
  • 无障碍访问需添加 aria-expanded 等 ARIA 属性

vue实现折叠组件

标签: 组件vue
分享给朋友:

相关文章

vue 实现目录

vue 实现目录

Vue 实现目录的方法 在 Vue 项目中实现目录功能,可以通过以下几种方式实现,具体取决于需求和技术栈。 使用 Vue Router 实现路由目录 Vue Router 是 Vue.js 的官方路…

vue实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如a…

vue实现图片分析

vue实现图片分析

Vue 实现图片分析的方法 在 Vue 中实现图片分析通常涉及图片上传、预览、后端交互以及分析结果展示等功能。以下是几种常见的实现方式: 使用 HTML5 File API 和 Canvas 进行…

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue实现tab换行

vue实现tab换行

Vue实现Tab切换的常见方法 使用Vue实现Tab切换功能可以通过多种方式完成,以下是几种常见的方法: 动态组件结合v-if或v-show 通过v-if或v-show控制不同Tab内容的显示…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…