当前位置:首页 > 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实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newIte…

vue 实现原理

vue 实现原理

Vue 实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现原理主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现原理的核心要点: 响应式系统 Vu…

vue 实现获取焦点

vue 实现获取焦点

实现获取焦点的方法 在 Vue 中实现获取焦点可以通过多种方式完成,以下是几种常见的方法: 使用 ref 和 focus 方法 通过 ref 属性获取 DOM 元素,并调用其 focus 方法实现焦…