当前位置:首页 > VUE

vue实现展开收起列表

2026-01-22 08:08:28VUE

Vue 实现展开收起列表

使用 v-ifv-show 控制显示

通过 v-ifv-show 动态控制列表项的显示与隐藏。v-if 会销毁和重建 DOM 元素,适合切换频率较低的场景;v-show 通过 CSS 的 display 属性控制,适合频繁切换的场景。

<template>
  <div>
    <button @click="toggleList">展开/收起</button>
    <ul v-show="isExpanded">
      <li v-for="item in list" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

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

使用 CSS 过渡动画

通过 Vue 的 <transition> 组件实现平滑的展开/收起动画效果。

<template>
  <div>
    <button @click="toggleList">展开/收起</button>
    <transition name="slide">
      <ul v-show="isExpanded">
        <li v-for="item in list" :key="item.id">{{ item.name }}</li>
      </ul>
    </transition>
  </div>
</template>

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

动态计算高度

对于高度不确定的列表,可以通过 JavaScript 动态计算高度并应用动画。

<template>
  <div>
    <button @click="toggleList">展开/收起</button>
    <div ref="listContainer" :style="{ height: listHeight }">
      <ul>
        <li v-for="item in list" :key="item.id">{{ item.name }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      list: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
      ],
      listHeight: '0px',
    };
  },
  methods: {
    toggleList() {
      this.isExpanded = !this.isExpanded;
      if (this.isExpanded) {
        this.listHeight = `${this.$refs.listContainer.scrollHeight}px`;
      } else {
        this.listHeight = '0px';
      }
    },
  },
};
</script>

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

使用第三方库

对于更复杂的动画效果,可以借助第三方库如 vue-animate-height

vue实现展开收起列表

<template>
  <div>
    <button @click="toggleList">展开/收起</button>
    <vue-animate-height :duration="300" :height="height">
      <ul>
        <li v-for="item in list" :key="item.id">{{ item.name }}</li>
      </ul>
    </vue-animate-height>
  </div>
</template>

<script>
import VueAnimateHeight from 'vue-animate-height';
export default {
  components: { VueAnimateHeight },
  data() {
    return {
      isExpanded: false,
      list: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
      ],
    };
  },
  computed: {
    height() {
      return this.isExpanded ? 'auto' : 0;
    },
  },
  methods: {
    toggleList() {
      this.isExpanded = !this.isExpanded;
    },
  },
};
</script>

以上方法可以根据实际需求选择,实现简单或复杂的展开收起效果。

标签: 列表vue
分享给朋友:

相关文章

vue实现多选div

vue实现多选div

实现多选 DIV 的基本思路 在 Vue 中实现多选 DIV 功能,可以通过以下方法实现。主要思路是利用 v-model 绑定选中状态,结合 CSS 类名动态切换样式。 数据准备与模板结构 定义一个…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…