当前位置:首页 > VUE

vue实现伸缩列表

2026-03-10 01:17:58VUE

实现伸缩列表的基本思路

使用Vue实现伸缩列表的核心在于动态控制列表项的显示与隐藏。可以通过v-for渲染列表,结合v-showv-if控制子项的展开状态,并利用CSS过渡效果提升交互体验。

基础实现方法

模板结构

<template>
  <div class="expandable-list">
    <div 
      v-for="(item, index) in listData" 
      :key="index" 
      class="list-item"
    >
      <div class="header" @click="toggleExpand(index)">
        {{ item.title }}
        <span class="arrow">{{ isExpanded(index) ? '↑' : '↓' }}</span>
      </div>
      <div class="content" v-show="expandedItems.includes(index)">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

脚本逻辑

vue实现伸缩列表

<script>
export default {
  data() {
    return {
      listData: [
        { title: 'Item 1', content: 'Content for item 1' },
        { title: 'Item 2', content: 'Content for item 2' }
      ],
      expandedItems: []
    }
  },
  methods: {
    toggleExpand(index) {
      const position = this.expandedItems.indexOf(index)
      position === -1 
        ? this.expandedItems.push(index) 
        : this.expandedItems.splice(position, 1)
    },
    isExpanded(index) {
      return this.expandedItems.includes(index)
    }
  }
}
</script>

样式设计

<style scoped>
.list-item {
  border: 1px solid #eee;
  margin-bottom: 8px;
  border-radius: 4px;
}
.header {
  padding: 12px;
  background: #f5f5f5;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
}
.content {
  padding: 12px;
  transition: all 0.3s ease;
}
.arrow {
  margin-left: 8px;
}
</style>

进阶优化方案

使用组件化设计 将列表项拆分为独立组件,提高代码复用性:

vue实现伸缩列表

<template>
  <expandable-item 
    v-for="(item, index) in listData"
    :key="index"
    :item="item"
    :is-expanded="expandedItems.includes(index)"
    @toggle="toggleExpand(index)"
  />
</template>

添加动画效果 利用Vue的过渡组件实现平滑展开:

<transition name="slide">
  <div class="content" v-show="isExpanded">
    <slot></slot>
  </div>
</transition>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.3s ease;
}
.slide-enter, .slide-leave-to {
  max-height: 0;
  overflow: hidden;
}
.slide-enter-to, .slide-leave {
  max-height: 500px;
}

性能优化建议

对于大型列表,使用v-show代替v-if可以减少DOM操作。考虑虚拟滚动技术处理超长列表,避免同时渲染大量隐藏内容。

第三方库方案

考虑使用现成组件库加速开发:

  • Vuetify的v-expansion-panels
  • Element UI的el-collapse
  • Ant Design Vue的a-collapse

这些组件提供开箱即用的伸缩面板功能,支持手风琴模式、自定义图标等高级特性。

标签: 伸缩列表
分享给朋友:

相关文章

vue实现列表全选

vue实现列表全选

实现全选功能的基本思路 在Vue中实现列表全选功能,通常需要结合v-model和计算属性来管理选中状态。全选按钮的状态与列表中所有项的选中状态相互关联。 基础实现方法 准备一个数据列表和对应的选中状…

vue视频列表实现

vue视频列表实现

Vue 视频列表实现 数据准备 定义一个数组存储视频信息,每个视频对象包含标题、封面图、播放地址等属性。 data() { return { videos: [ {…

vue实现列表展示

vue实现列表展示

Vue 实现列表展示 在 Vue 中实现列表展示通常使用 v-for 指令,结合数据绑定和动态渲染功能。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,动态生成列表项。假…

vue实现列表排序

vue实现列表排序

实现列表排序的方法 在Vue中实现列表排序可以通过多种方式完成,以下是几种常见的方法: 使用JavaScript的sort方法 在Vue的methods中定义一个排序函数,利用JavaScript…

vue实现列表翻页

vue实现列表翻页

Vue 列表翻页实现方法 基础分页实现 在 Vue 中实现列表翻页通常需要结合分页组件和数据请求。使用 Element UI 的分页组件可以快速实现: <template> <…

vue实现列表连线

vue实现列表连线

实现列表连线的基本思路 在Vue中实现列表连线效果,可以通过动态渲染DOM元素并结合CSS样式来完成。关键在于获取列表项的位置信息,并通过计算连线路径。 使用CSS和伪元素实现简单连线 对于简单的垂…