当前位置:首页 > 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 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…

vue 实现商品列表

vue 实现商品列表

实现商品列表的基本结构 在Vue中实现商品列表,通常需要创建一个组件来展示商品数据。可以使用v-for指令循环渲染商品列表,并通过数据绑定动态显示商品信息。 <template> &…

vue实现滚动列表

vue实现滚动列表

Vue 实现滚动列表的方法 使用 CSS 实现基础滚动 通过 CSS 的 overflow 属性可以快速实现滚动效果。适用于静态列表或简单动态内容。 <template> <d…

vue实现索引列表

vue实现索引列表

Vue 实现索引列表 使用第三方库(如 better-scroll 或 vue-index-list) 安装 better-scroll 或 vue-index-list 库,可以快速实现带索引的列表…

vue实现横向列表

vue实现横向列表

实现横向列表的方法 在Vue中实现横向列表可以通过CSS样式控制列表项的排列方式。横向列表通常用于导航菜单、图片展示等场景。 使用flex布局实现 flex布局是实现横向列表最简单的方法之一,通过设…