当前位置:首页 > VUE

vue实现伸缩列表

2026-01-17 19:34:27VUE

Vue 实现伸缩列表的方法

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

动态绑定高度

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

vue实现伸缩列表

<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

vue实现伸缩列表

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 属性也可以实现平滑过渡:

<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中实现列表权限通常涉及根据用户角色或权限动态渲染列表内容。核心逻辑是通过权限判断控制列表项的显示、隐藏或操作权限。 权限数据管理 使用Vuex或Pinia存储全局权…

vue实现列表显示

vue实现列表显示

使用 Vue 实现列表显示 基础列表渲染 在 Vue 中,可以通过 v-for 指令实现列表渲染。以下是一个简单的示例: <template> <ul> <…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象进行渲染。以下是几种常见的实现方式: 基础列表渲染 通过 v-for 指令遍历数组,动态生成列表项。 &…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaS…

vue列表查询实现

vue列表查询实现

实现Vue列表查询功能 基本数据绑定与渲染 在Vue中实现列表查询,首先需要定义数据源和查询条件。通过v-model绑定搜索输入框,使用计算属性过滤列表。 <template> &…

jquery 列表

jquery 列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>)。以下是一些常见的操作方式: 动态添加列表项 使用 append()…