当前位置:首页 > 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 方法则更现代简洁。

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

相关文章

h5 实现列表

h5 实现列表

列表实现方法 在HTML5中,可以通过多种方式实现列表,包括无序列表、有序列表和自定义列表。以下是具体的实现方法。 无序列表 无序列表使用<ul>标签,列表项使用<li>标签…

jquery列表

jquery列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>),包括动态添加、删除、修改列表项,以及事件绑定等。 创建列表 使用 j…

vue 实现列表多选

vue 实现列表多选

Vue 实现列表多选的方法 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动处理多选逻辑。适用于复选框(checkbox)场景。 <template> &…

vue实现列表分类

vue实现列表分类

Vue 实现列表分类的方法 使用计算属性分类 通过计算属性对原始数组进行分类处理,返回分组后的对象或数组。适用于静态数据或需要响应式更新的场景。 computed: { categorizedI…

vue实现虚拟列表

vue实现虚拟列表

虚拟列表的概念 虚拟列表(Virtual List)是一种优化长列表渲染性能的技术,通过仅渲染可视区域内的元素,减少DOM节点数量,从而提升页面性能。适用于数据量大的场景(如表格、下拉选择器等)。…

vue如何实现列表

vue如何实现列表

使用 v-for 指令渲染列表 在 Vue 中,可以通过 v-for 指令动态渲染列表数据。v-for 需要特殊的语法形式 item in items,其中 items 是源数据数组,item 是数组…