当前位置:首页 > VUE

vue实现伸缩列表

2026-01-17 19:34:27VUE

Vue 实现伸缩列表的方法

使用 Vue 实现伸缩列表可以通过动态绑定样式或类名,结合过渡效果实现平滑的展开和收起动画。以下是几种常见实现方式:

动态绑定高度

通过 v-ifv-show 控制列表显示,结合 CSS 过渡实现动画效果:

<template>
  <div>
    <button @click="toggleList">切换列表</button>
    <transition name="slide">
      <ul v-show="isExpanded">
        <li v-for="item in items" :key="item.id">{{ item.text }}</li>
      </ul>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      items: [
        { id: 1, text: 'Item 1' },
        { id: 2, text: 'Item 2' }
      ]
    }
  },
  methods: {
    toggleList() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.5s ease;
  overflow: hidden;
}
.slide-enter, .slide-leave-to {
  max-height: 0;
}
.slide-enter-to, .slide-leave {
  max-height: 500px;
}
</style>

使用第三方组件

Vue 生态中有现成的可伸缩列表组件,如 vue-accordion

npm install vue-accordion
<template>
  <vue-accordion>
    <vue-accordion-item v-for="item in items" :key="item.id">
      <template v-slot:title>{{ item.title }}</template>
      <template v-slot:content>{{ item.content }}</template>
    </vue-accordion-item>
  </vue-accordion>
</template>

<script>
import { VueAccordion, VueAccordionItem } from 'vue-accordion'

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

动态计算内容高度

对于需要精确控制高度的场景,可以通过 ref 获取元素实际高度:

<template>
  <div>
    <button @click="toggle">展开/收起</button>
    <div ref="content" :style="{ height: contentHeight }">
      <p>列表内容...</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      contentHeight: '0px'
    }
  },
  methods: {
    toggle() {
      if (this.isExpanded) {
        this.contentHeight = '0px'
      } else {
        this.contentHeight = `${this.$refs.content.scrollHeight}px`
      }
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style>
div {
  transition: height 0.3s ease;
  overflow: hidden;
}
</style>

使用 CSS Grid 布局

CSS Grid 的 grid-template-rows 属性也可以实现平滑过渡:

vue实现伸缩列表

<template>
  <div class="expandable" :class="{ expanded: isExpanded }">
    <div class="content">
      <p>列表内容...</p>
    </div>
    <button @click="isExpanded = !isExpanded">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
  </div>
</template>

<style>
.expandable {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.3s ease;
}
.expandable.expanded {
  grid-template-rows: 1fr;
}
.content {
  overflow: hidden;
}
</style>

以上方法可以根据具体需求选择,动态高度方法适用于内容高度不固定的场景,CSS Grid 方法则更现代简洁。

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

相关文章

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 无限滚动列表通常用于展示大量数据,通过动态加载数据减少初始渲染压力。以下是基于Vue的实现方法: 使用Intersection Observer API监听滚动 Inters…

vue实现列表全选

vue实现列表全选

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

vue实现列表页面

vue实现列表页面

Vue 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…

vue实现索引列表

vue实现索引列表

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

vue实现列表连线

vue实现列表连线

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

vue实现伸缩收回

vue实现伸缩收回

实现伸缩收回的常见方法 在Vue中实现伸缩收回功能,可以通过多种方式实现,以下是几种常见的方法: 使用v-show或v-if控制显示隐藏 通过Vue的指令v-show或v-if可以简单地控制元素的显…