当前位置:首页 > 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>

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

<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 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: n…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript 图表…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…