当前位置:首页 > VUE

vue实现列表展开收缩

2026-01-21 12:19:02VUE

Vue 实现列表展开收缩的方法

使用 v-show 或 v-if 控制显示

通过 Vue 的指令 v-showv-if 可以动态控制列表项的显示与隐藏。v-show 通过 CSS 的 display 属性切换,适合频繁切换的场景;v-if 会销毁和重建 DOM 节点,适合不频繁切换的场景。

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

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

使用动态 class 或 style 实现动画效果

结合 CSS 过渡或动画,可以为展开和收缩添加平滑效果。通过绑定动态 class 或 style 实现高度变化。

<template>
  <div>
    <button @click="toggleList">切换列表</button>
    <ul :class="{ 'expanded': isExpanded, 'collapsed': !isExpanded }">
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

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

<style>
.expanded {
  max-height: 500px;
  transition: max-height 0.5s ease-in;
}

.collapsed {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.5s ease-out;
}
</style>

使用第三方动画库(如 Vue Transition)

Vue 的 <transition> 组件可以更方便地实现动画效果,支持多种动画钩子。

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

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

<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>

使用组件封装实现复用

将展开收缩功能封装为可复用的组件,方便在多个地方调用。

<template>
  <div>
    <button @click="toggle">切换</button>
    <div ref="content" class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    };
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded;
      const content = this.$refs.content;
      content.style.maxHeight = this.isExpanded ? `${content.scrollHeight}px` : '0';
    }
  }
};
</script>

<style>
.content {
  overflow: hidden;
  transition: max-height 0.3s ease;
}
</style>

调用时只需传入列表内容即可:

vue实现列表展开收缩

<template>
  <expandable-list>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
  </expandable-list>
</template>

以上方法可以根据实际需求选择,灵活实现列表的展开与收缩功能。

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

相关文章

vue实现积分

vue实现积分

Vue 实现积分功能 在 Vue 中实现积分功能通常涉及前端展示、交互逻辑以及与后端 API 的通信。以下是常见的实现方法和步骤: 积分展示组件 创建一个积分展示组件,用于显示当前用户的积分。可以使…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现建模

vue实现建模

Vue 实现建模的方法 在 Vue 中实现建模通常涉及数据绑定、组件化和状态管理。以下是几种常见的方法: 数据驱动建模 Vue 的核心是数据驱动视图。通过定义数据模型,Vue 会自动更新 DOM。例…

vue实现项目

vue实现项目

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 创建 Vue 项目 使用 Vue CLI 快…

vue实现拉伸

vue实现拉伸

Vue 实现元素拉伸功能 在Vue中实现元素的拉伸(拖拽调整大小)功能,可以通过监听鼠标事件结合CSS样式来实现。以下是两种常见实现方式: 使用原生事件监听 创建可拉伸的组件需要处理鼠标按下…

vue实现选择置顶

vue实现选择置顶

Vue 实现选择置顶功能 在 Vue 中实现选择置顶功能,可以通过操作数据数组或结合 CSS 样式来实现。以下是几种常见的实现方法: 数组排序法 通过操作数据数组,将被选中的项移动到数组首位: /…