当前位置:首页 > VUE

vue实现收缩展开

2026-03-29 15:04:49VUE

Vue 实现收缩展开功能

使用 v-ifv-show 控制显示

通过 Vue 的指令 v-ifv-show 可以轻松实现元素的显示与隐藏。v-if 会动态添加或移除 DOM 元素,而 v-show 仅切换 CSS 的 display 属性。

<template>
  <div>
    <button @click="toggle">Toggle Content</button>
    <div v-if="isVisible">This content can be toggled.</div>
  </div>
</template>

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

使用 CSS 过渡动画

为收缩展开效果添加平滑的过渡动画,可以使用 Vue 的 <transition> 组件结合 CSS 过渡。

vue实现收缩展开

<template>
  <div>
    <button @click="toggle">Toggle with Animation</button>
    <transition name="fade">
      <div v-if="isVisible" class="content">Content with fade effect.</div>
    </transition>
  </div>
</template>

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

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

动态高度过渡

如果需要实现高度从 0 到 auto 的过渡,可以使用 max-height 结合 CSS 过渡。

vue实现收缩展开

<template>
  <div>
    <button @click="toggle">Toggle Height</button>
    <div class="collapse-container">
      <div class="collapse-content" :style="{ maxHeight: isVisible ? '1000px' : '0' }">
        Content with height transition.
      </div>
    </div>
  </div>
</template>

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

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

使用第三方库

如果需要更复杂的功能,可以借助第三方库如 vue-collapse 或手动封装组件。

<template>
  <div>
    <button @click="toggle">Toggle with Library</button>
    <vue-collapse :show="isVisible">
      <div>Content collapsed by vue-collapse.</div>
    </vue-collapse>
  </div>
</template>

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

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

封装可复用组件

将收缩展开功能封装为可复用的组件,便于在项目中多次使用。

<template>
  <div>
    <button @click="toggle">{{ buttonText }}</button>
    <div class="collapse-wrapper" :style="{ height: isOpen ? 'auto' : '0' }">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    buttonText: {
      type: String,
      default: 'Toggle'
    }
  },
  data() {
    return {
      isOpen: false
    }
  },
  methods: {
    toggle() {
      this.isOpen = !this.isOpen
    }
  }
}
</script>

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

以上方法涵盖了从基础到进阶的实现方式,根据需求选择合适的方式即可。

标签: vue
分享给朋友:

相关文章

vue路由实现内部切换

vue路由实现内部切换

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

vue异步组建实现原理

vue异步组建实现原理

Vue 异步组件实现原理 Vue 的异步组件允许将组件按需加载,通常用于代码分割或延迟加载非关键组件。其核心原理基于 Promise 和动态导入。 基本实现方式 通过 defineAsyncComp…

vue 动画实现

vue 动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要分为内置组件和第三方库集成。 使用 Vue 内置过渡组件 Vue 的 <transition> 和 <transiti…

vue实现swiper

vue实现swiper

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

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue实现checkbox

vue实现checkbox

Vue 实现 Checkbox 在 Vue 中实现 Checkbox 可以通过原生 HTML 的 <input type="checkbox"> 或使用 Vue 的 v-model 指令绑…