当前位置:首页 > 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> 组件可以为展开收起添加平滑的动画效果。

<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。安装后可以直接使用其提供的组件。

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 可以实现更平滑的展开收起动画,避免内容高度不确定的问题。

vue展开收起功能实现

<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 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例: &…

vue实现报表

vue实现报表

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

vue实现效果

vue实现效果

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

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…