当前位置:首页 > VUE

vue展开收起功能实现

2026-01-20 18:32:15VUE

Vue 展开收起功能实现

使用 v-show 或 v-if 控制显示隐藏

通过 Vue 的指令 v-showv-if 可以轻松实现展开收起功能。v-show 通过 CSS 的 display 属性控制元素显示隐藏,适合频繁切换的场景;v-if 会直接销毁和重建 DOM,适合不频繁切换的场景。

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <div v-show="isExpanded">
      这里是需要展开收起的内容
    </div>
  </div>
</template>

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

使用 transition 添加动画效果

通过 Vue 的 <transition> 组件可以为展开收起添加平滑的动画效果。

vue展开收起功能实现

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <transition name="fade">
      <div v-show="isExpanded">
        这里是需要展开收起的内容
      </div>
    </transition>
  </div>
</template>

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

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s, max-height 0.5s;
  max-height: 500px;
  overflow: hidden;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
  max-height: 0;
}
</style>

使用第三方库如 vue-collapse

对于更复杂的展开收起需求,可以使用第三方库如 vue-collapse。安装后可以直接使用其提供的组件。

vue展开收起功能实现

npm install vue-collapse --save
<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <vue-collapse v-model="isExpanded">
      <div>
        这里是需要展开收起的内容
      </div>
    </vue-collapse>
  </div>
</template>

<script>
import VueCollapse from 'vue-collapse'

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

动态控制内容高度

通过动态绑定样式,可以实现内容高度的平滑过渡。

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <div :style="{ height: isExpanded ? 'auto' : '0', overflow: 'hidden', transition: 'height 0.5s' }">
      这里是需要展开收起的内容
    </div>
  </div>
</template>

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

使用 CSS 的 max-height 实现动画

通过设置 max-height 可以实现更平滑的展开收起动画,避免内容高度不确定的问题。

<template>
  <div>
    <button @click="toggle">点击{{ isExpanded ? '收起' : '展开' }}</button>
    <div class="content" :class="{ 'expanded': isExpanded }">
      这里是需要展开收起的内容
    </div>
  </div>
</template>

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

<style>
.content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.5s ease;
}
.content.expanded {
  max-height: 500px;
}
</style>

以上方法可以根据具体需求选择适合的方式实现展开收起功能。

标签: 功能vue
分享给朋友:

相关文章

vue表格实现教学

vue表格实现教学

Vue 表格实现方法 使用原生HTML表格 在Vue模板中直接使用HTML的<table>标签,结合v-for动态渲染数据: <template> <table&…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-i…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使…

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 wi…