当前位置:首页 > VUE

vue实现折叠效果

2026-01-16 17:56:23VUE

使用 v-showv-if 实现基础折叠

通过 Vue 的指令 v-showv-if 控制元素的显示与隐藏。v-show 通过 CSS 的 display 属性切换,v-if 直接操作 DOM。

<template>
  <div>
    <button @click="isCollapsed = !isCollapsed">切换折叠</button>
    <div v-show="!isCollapsed">可折叠的内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapsed: false
    };
  }
};
</script>

结合 CSS 过渡动画

通过 Vue 的 <transition> 组件和 CSS 实现平滑的折叠动画效果。

<template>
  <div>
    <button @click="isCollapsed = !isCollapsed">切换折叠</button>
    <transition name="slide">
      <div v-show="!isCollapsed" class="content">带动画的折叠内容</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapsed: false
    };
  }
};
</script>

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

使用第三方库(如 vue-collapse

对于更复杂的折叠需求,可以集成第三方库如 vue-collapse,提供更多功能(如手风琴效果)。

安装库:

npm install vue-collapse

示例代码:

<template>
  <div>
    <vue-collapse v-model="activePanels">
      <vue-collapse-item title="标题1" name="1">内容1</vue-collapse-item>
      <vue-collapse-item title="标题2" name="2">内容2</vue-collapse-item>
    </vue-collapse>
  </div>
</template>

<script>
import { VueCollapse, VueCollapseItem } from 'vue-collapse';

export default {
  components: { VueCollapse, VueCollapseItem },
  data() {
    return {
      activePanels: ['1']
    };
  }
};
</script>

动态生成折叠面板

通过 v-for 动态渲染多个折叠项,并管理各自的状态。

<template>
  <div>
    <div v-for="(item, index) in items" :key="index">
      <button @click="toggleItem(index)">{{ item.title }}</button>
      <div v-show="item.isOpen">{{ item.content }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { title: '标题1', content: '内容1', isOpen: false },
        { title: '标题2', content: '内容2', isOpen: false }
      ]
    };
  },
  methods: {
    toggleItem(index) {
      this.items[index].isOpen = !this.items[index].isOpen;
    }
  }
};
</script>

手风琴效果(单一展开)

确保每次只展开一个折叠项,通过维护当前展开的索引实现。

<template>
  <div>
    <div v-for="(item, index) in items" :key="index">
      <button @click="setActive(index)">{{ item.title }}</button>
      <div v-show="activeIndex === index">{{ item.content }}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: null,
      items: [
        { title: '标题1', content: '内容1' },
        { title: '标题2', content: '内容2' }
      ]
    };
  },
  methods: {
    setActive(index) {
      this.activeIndex = this.activeIndex === index ? null : index;
    }
  }
};
</script>

vue实现折叠效果

标签: 效果vue
分享给朋友:

相关文章

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-li…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: data…

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于…