当前位置:首页 > VUE

vue列表展开实现

2026-01-17 12:48:19VUE

Vue 列表展开实现方法

使用 v-for 和 v-show 控制展开状态

通过 v-for 渲染列表项,配合 v-show 控制子内容的显示隐藏。需要为每个列表项维护一个展开状态。

vue列表展开实现

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

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, title: '标题1', content: '内容1' },
        { id: 2, title: '标题2', content: '内容2' }
      ],
      expandedItems: []
    }
  },
  methods: {
    toggleExpand(id) {
      const index = this.expandedItems.indexOf(id)
      if (index > -1) {
        this.expandedItems.splice(index, 1)
      } else {
        this.expandedItems.push(id)
      }
    }
  }
}
</script>

使用动态组件实现展开效果

通过动态组件可以更灵活地控制展开内容的显示方式。

vue列表展开实现

<template>
  <div>
    <div v-for="item in items" :key="item.id">
      <div @click="currentExpanded = currentExpanded === item.id ? null : item.id">
        {{ item.title }}
      </div>
      <transition name="fade">
        <div v-if="currentExpanded === item.id">
          {{ item.content }}
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, title: '标题1', content: '内容1' },
        { id: 2, title: '标题2', content: '内容2' }
      ],
      currentExpanded: null
    }
  }
}
</script>

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

使用第三方组件库实现

Element UI 等组件库提供了现成的展开组件,可以快速实现需求。

<template>
  <el-collapse v-model="activeNames">
    <el-collapse-item 
      v-for="item in items" 
      :key="item.id" 
      :title="item.title" 
      :name="item.id"
    >
      {{ item.content }}
    </el-collapse-item>
  </el-collapse>
</template>

<script>
export default {
  data() {
    return {
      activeNames: [],
      items: [
        { id: 1, title: '标题1', content: '内容1' },
        { id: 2, title: '标题2', content: '内容2' }
      ]
    }
  }
}
</script>

使用 CSS 过渡效果增强用户体验

为展开动作添加平滑的过渡效果,提升用户体验。

<template>
  <div>
    <div v-for="item in items" :key="item.id">
      <div @click="toggleExpand(item.id)" class="title">
        {{ item.title }}
      </div>
      <div 
        class="content" 
        :class="{ 'expanded': expandedItems.includes(item.id) }"
      >
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<style>
.content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
}
.content.expanded {
  max-height: 500px;
  transition: max-height 0.5s ease-in;
}
</style>

以上方法可以根据具体需求选择使用,从简单到复杂提供了多种实现方案。

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

相关文章

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…

vue 实现筛选

vue 实现筛选

实现筛选功能的基本方法 在Vue中实现筛选功能通常涉及以下几个核心步骤: 数据绑定与筛选逻辑 使用v-model绑定筛选条件到Vue实例的数据属性,结合计算属性实现动态筛选: data() {…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…

vue实现word文档实现预览

vue实现word文档实现预览

在Vue中实现Word文档预览可以通过多种方式实现,以下是几种常见的方法: 使用mammoth.js库 mammoth.js是一个将Word文档(.docx)转换为HTML的JavaScript库,…

vue 实现管理系统

vue 实现管理系统

Vue 实现管理系统的基本步骤 使用 Vue 实现管理系统需要结合前端框架、UI 组件库和后端接口。以下是一个常见的实现方案。 技术选型 前端框架:Vue 3(Composition API)或 V…