当前位置:首页 > VUE

vue列表展开实现

2026-01-17 12:48:19VUE

Vue 列表展开实现方法

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

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

<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>

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

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

<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 过渡效果增强用户体验

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

vue列表展开实现

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

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…