当前位置:首页 > VUE

vue实现收缩展开

2026-03-09 03:35:13VUE

实现收缩展开的基本思路

在Vue中实现收缩展开功能,可以通过控制CSS类或内联样式来实现元素的显示与隐藏。常用的方法包括使用v-showv-if指令,或者通过动态绑定CSS类来实现动画效果。

使用v-show实现简单切换

v-show通过切换元素的display属性来控制显示与隐藏,适合频繁切换的场景。

<template>
  <div>
    <button @click="isCollapsed = !isCollapsed">Toggle</button>
    <div v-show="!isCollapsed">Content to collapse/expand</div>
  </div>
</template>

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

使用v-if实现条件渲染

v-if会完全销毁或重新创建DOM元素,适合不频繁切换且需要条件渲染的场景。

vue实现收缩展开

<template>
  <div>
    <button @click="isCollapsed = !isCollapsed">Toggle</button>
    <div v-if="!isCollapsed">Content to collapse/expand</div>
  </div>
</template>

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

添加过渡动画效果

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

<template>
  <div>
    <button @click="isCollapsed = !isCollapsed">Toggle</button>
    <transition name="fade">
      <div v-show="!isCollapsed">Content with animation</div>
    </transition>
  </div>
</template>

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

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

动态绑定高度实现平滑展开

通过动态绑定元素的高度,可以实现更平滑的展开效果。

vue实现收缩展开

<template>
  <div>
    <button @click="toggleCollapse">Toggle</button>
    <div 
      ref="content"
      :style="{ height: isCollapsed ? '0px' : contentHeight + 'px' }"
      class="collapsible-content"
    >
      Content with height transition
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapsed: false,
      contentHeight: 0
    }
  },
  mounted() {
    this.contentHeight = this.$refs.content.scrollHeight;
  },
  methods: {
    toggleCollapse() {
      this.isCollapsed = !this.isCollapsed;
    }
  }
}
</script>

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

使用第三方库实现高级效果

对于更复杂的收缩展开需求,可以使用第三方动画库如vue-animate-onscrollanimate.css

<template>
  <div>
    <button @click="isCollapsed = !isCollapsed">Toggle</button>
    <div 
      v-show="!isCollapsed"
      class="animate__animated animate__fadeIn"
    >
      Content with Animate.css
    </div>
  </div>
</template>

<script>
import 'animate.css';
export default {
  data() {
    return {
      isCollapsed: false
    }
  }
}
</script>

注意事项

动态内容高度变化时,需要重新计算高度值。可以在updated钩子中更新contentHeight

updated() {
  if (!this.isCollapsed) {
    this.contentHeight = this.$refs.content.scrollHeight;
  }
}

对于嵌套的收缩展开组件,建议使用事件总线或Vuex管理状态,避免直接修改父组件状态。

标签: vue
分享给朋友:

相关文章

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现分类

vue实现分类

Vue实现分类功能的方法 使用v-for和计算属性 通过计算属性对数据进行分类处理,结合v-for渲染分类后的列表。示例代码: <template> <div>…

vue实现管道

vue实现管道

Vue 中实现管道(Pipe)功能 在 Vue 中可以通过过滤器(Filters)或计算属性(Computed Properties)实现类似管道的功能,将数据经过多个处理步骤后输出。 使用过滤器(…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…

vue实现 treegrid

vue实现 treegrid

Vue 实现 TreeGrid 的方法 使用现成组件库 Element UI、Ant Design Vue 等流行组件库提供了现成的 TreeTable/TreeGrid 组件,可直接使用。 Ele…