当前位置:首页 > VUE

vue列表展开实现

2026-03-09 18:16:00VUE

实现 Vue 列表展开功能

在 Vue 中实现列表展开功能通常涉及动态控制列表项的显示与隐藏。以下是几种常见方法:

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

通过绑定布尔值控制子列表的展开状态:

vue列表展开实现

<template>
  <div>
    <div v-for="item in list" :key="item.id">
      <div @click="item.expanded = !item.expanded">
        {{ item.title }}
      </div>
      <div v-show="item.expanded">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { id: 1, title: 'Item 1', content: 'Content 1', expanded: false },
        { id: 2, title: 'Item 2', content: 'Content 2', expanded: false }
      ]
    }
  }
}
</script>

使用计算属性实现手风琴效果

确保同时只展开一个列表项:

vue列表展开实现

<template>
  <div>
    <div v-for="item in list" :key="item.id">
      <div @click="toggleExpand(item.id)">
        {{ item.title }}
      </div>
      <div v-show="expandedItem === item.id">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      expandedItem: null,
      list: [
        { id: 1, title: 'Item 1', content: 'Content 1' },
        { id: 2, title: 'Item 2', content: 'Content 2' }
      ]
    }
  },
  methods: {
    toggleExpand(id) {
      this.expandedItem = this.expandedItem === id ? null : id
    }
  }
}
</script>

使用 transition 添加动画效果

为展开/收起过程添加平滑过渡:

<template>
  <div>
    <div v-for="item in list" :key="item.id">
      <div @click="item.expanded = !item.expanded">
        {{ item.title }}
      </div>
      <transition name="fade">
        <div v-show="item.expanded">
          {{ item.content }}
        </div>
      </transition>
    </div>
  </div>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

使用第三方组件库

Element UI 或 Ant Design Vue 等库提供现成的展开组件:

<template>
  <el-collapse v-model="activeNames">
    <el-collapse-item title="Item 1" name="1">
      Content 1
    </el-collapse-item>
    <el-collapse-item title="Item 2" name="2">
      Content 2
    </el-collapse-item>
  </el-collapse>
</template>

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

这些方法可根据具体需求选择使用,从简单显示隐藏到带动画的复杂交互都能实现。

标签: 列表vue
分享给朋友:

相关文章

vue使用vr实现标注

vue使用vr实现标注

Vue 中使用 VR 实现标注的方法 在 Vue 项目中结合 VR 技术实现标注功能,可以通过 WebXR 和 Three.js 等库实现。以下为具体实现方法: 安装依赖 确保项目中已安装 Thre…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="con…