当前位置:首页 > 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.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model…