当前位置:首页 > VUE

vue 实现口风琴

2026-01-16 01:15:57VUE

Vue 实现手风琴效果

手风琴效果(Accordion)是一种常见的UI交互模式,允许用户展开或折叠内容区域。以下是几种实现方式:

使用v-for和v-show

<template>
  <div class="accordion">
    <div v-for="(item, index) in items" :key="index" class="accordion-item">
      <div class="accordion-header" @click="toggle(index)">
        {{ item.title }}
      </div>
      <div class="accordion-content" v-show="activeIndex === index">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: null,
      items: [
        { title: '标题1', content: '内容1' },
        { title: '标题2', content: '内容2' },
        { title: '标题3', content: '内容3' }
      ]
    }
  },
  methods: {
    toggle(index) {
      this.activeIndex = this.activeIndex === index ? null : index
    }
  }
}
</script>

<style>
.accordion-item {
  margin-bottom: 5px;
}
.accordion-header {
  padding: 10px;
  background: #eee;
  cursor: pointer;
}
.accordion-content {
  padding: 10px;
  border: 1px solid #ddd;
}
</style>

使用动态组件

<template>
  <div>
    <button 
      v-for="(tab, index) in tabs" 
      :key="index"
      @click="currentTab = index"
    >
      {{ tab.title }}
    </button>
    <component :is="tabs[currentTab].content" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0,
      tabs: [
        { title: 'Tab 1', content: 'TabContent1' },
        { title: 'Tab 2', content: 'TabContent2' }
      ]
    }
  },
  components: {
    TabContent1: { template: '<div>内容1</div>' },
    TabContent2: { template: '<div>内容2</div>' }
  }
}
</script>

使用第三方库

对于更复杂的需求,可以考虑使用现成的Vue组件库:

  • Element UI的ElCollapse组件
  • Vuetify的v-expansion-panels
  • Bootstrap Vue的b-collapse
<template>
  <el-collapse v-model="activeNames">
    <el-collapse-item title="标题1" name="1">
      <div>内容1</div>
    </el-collapse-item>
    <el-collapse-item title="标题2" name="2">
      <div>内容2</div>
    </el-collapse-item>
  </el-collapse>
</template>

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

动画效果增强

为手风琴添加平滑的展开/折叠动画:

<template>
  <div>
    <div v-for="(item, index) in items" :key="index">
      <div @click="toggle(index)" class="header">
        {{ item.title }}
      </div>
      <transition name="slide">
        <div v-show="activeIndex === index" class="content">
          {{ item.content }}
        </div>
      </transition>
    </div>
  </div>
</template>

<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;
}
</style>

多选模式实现

如果需要同时展开多个项目,可以修改toggle方法:

methods: {
  toggle(index) {
    if (this.activeIndexes.includes(index)) {
      this.activeIndexes = this.activeIndexes.filter(i => i !== index)
    } else {
      this.activeIndexes.push(index)
    }
  }
}

以上方法提供了从基础到进阶的手风琴实现方案,可根据具体需求选择合适的实现方式。

vue 实现口风琴

标签: 口风vue
分享给朋友:

相关文章

vue实现选区创建

vue实现选区创建

Vue 实现选区创建的方法 在 Vue 中实现选区创建通常涉及 DOM 操作和事件处理。以下是几种常见的方法: 使用原生 DOM API 通过 window.getSelection() 和 Ran…

vue实现前端分离

vue实现前端分离

Vue 实现前后端分离 前后端分离是一种架构模式,前端负责页面渲染和交互逻辑,后端提供数据接口。Vue.js 作为前端框架,非常适合实现前后端分离的开发模式。 核心实现步骤 创建 Vue 项目 使…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-t…

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="modal-…

vue实现列表显示

vue实现列表显示

Vue 实现列表显示的方法 在 Vue 中实现列表显示通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式。 基本列表渲染 通过 v-for 指令遍历数组,动态生成…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template> &…