当前位置:首页 > VUE

vue 实现折叠

2026-03-27 21:03:46VUE

Vue 实现折叠功能

折叠功能可以通过 Vue 的动态组件、条件渲染或第三方库实现。以下是几种常见方法:

使用 v-if 或 v-show 实现基础折叠

通过点击事件切换布尔值,控制内容的显示与隐藏:

<template>
  <div>
    <button @click="toggle">Toggle Collapse</button>
    <div v-if="isCollapsed">
      Content to be collapsed
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapsed: false
    }
  },
  methods: {
    toggle() {
      this.isCollapsed = !this.isCollapsed
    }
  }
}
</script>

添加过渡动画效果

Vue 的 <transition> 组件可以为折叠添加平滑动画:

<template>
  <div>
    <button @click="toggle">Toggle with Animation</button>
    <transition name="fade">
      <div v-if="isCollapsed" class="content">
        Animated content
      </div>
    </transition>
  </div>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
.content {
  background: #f0f0f0;
  padding: 10px;
}
</style>

使用第三方库(如 Element UI)

Element UI 提供现成的 Collapse 组件:

<template>
  <el-collapse v-model="activeNames">
    <el-collapse-item title="Title 1" name="1">
      Content for item 1
    </el-collapse-item>
    <el-collapse-item title="Title 2" name="2">
      Content for item 2
    </el-collapse-item>
  </el-collapse>
</template>

<script>
export default {
  data() {
    return {
      activeNames: ['1']
    }
  }
}
</script>

实现手风琴效果(Accordion)

多个折叠面板中只展开一个:

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

<script>
export default {
  data() {
    return {
      activeIndex: null,
      items: [
        { title: 'Item 1', content: 'Content 1' },
        { title: 'Item 2', content: 'Content 2' }
      ]
    }
  },
  methods: {
    toggle(index) {
      this.activeIndex = this.activeIndex === index ? null : index
    }
  }
}
</script>

使用 CSS 控制高度动画

通过动态绑定样式实现高度变化:

vue 实现折叠

<template>
  <div>
    <button @click="toggle">Toggle Height</button>
    <div 
      class="collapsible" 
      :style="{ height: isCollapsed ? 'auto' : '0' }"
    >
      Content with height transition
    </div>
  </div>
</template>

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

每种方法适用于不同场景,基础项目可使用原生 Vue 实现,复杂需求可考虑第三方组件库。动画效果可通过 CSS transitions 或 Vue 的 transition 组件增强用户体验。

标签: vue
分享给朋友:

相关文章

vue实现sso

vue实现sso

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

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue遮罩实现

vue遮罩实现

实现 Vue 遮罩的常见方法 使用 CSS 定位和透明度 创建一个全屏遮罩层,通过 CSS 固定定位覆盖整个视口,并设置半透明背景色。 <template> <div cl…