当前位置:首页 > VUE

vue中实现伸缩框

2026-02-20 19:32:42VUE

实现伸缩框的基本思路

在Vue中实现伸缩框可以通过CSS过渡效果结合Vue的数据绑定和条件渲染完成。核心是利用v-showv-if控制内容显示,通过CSS实现平滑的展开/收起动画。

使用CSS过渡实现基础伸缩

<template>
  <div class="expandable-box">
    <button @click="isExpanded = !isExpanded">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
    <div class="content" :class="{ expanded: isExpanded }">
      <!-- 内容区域 -->
      这里是可伸缩的内容...
    </div>
  </div>
</template>

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

<style>
.content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
}

.content.expanded {
  max-height: 500px; /* 设置为足够大的值 */
}
</style>

动态计算内容高度

对于不确定高度的内容,可以通过ref获取DOM元素的实际高度:

vue中实现伸缩框

<template>
  <div class="expandable-box">
    <button @click="toggleExpand">
      {{ isExpanded ? '收起' : '展开' }}
    </button>
    <div ref="content" class="content" :style="{ height: contentHeight }">
      <!-- 可变高度的内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false,
      contentHeight: '0px'
    }
  },
  methods: {
    toggleExpand() {
      if (!this.isExpanded) {
        this.contentHeight = `${this.$refs.content.scrollHeight}px`
      } else {
        this.contentHeight = '0px'
      }
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

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

使用第三方组件库

对于更复杂的需求,可以使用现成的UI组件库:

  1. Element UI的Collapse组件:

    vue中实现伸缩框

    <el-collapse v-model="activeNames">
    <el-collapse-item title="标题" name="1">
     内容区域
    </el-collapse-item>
    </el-collapse>
  2. Ant Design Vue的Collapse组件:

    <a-collapse v-model:activeKey="activeKey">
    <a-collapse-panel key="1" header="标题">
     内容区域
    </a-collapse-panel>
    </a-collapse>

高级动画效果

使用Vue的<transition>组件实现更丰富的动画:

<template>
  <div>
    <button @click="show = !show">切换</button>
    <transition
      name="expand"
      @enter="enter"
      @after-enter="afterEnter"
      @leave="leave"
    >
      <div v-if="show" class="content">
        动画过渡的内容...
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      show: false
    }
  },
  methods: {
    enter(el) {
      el.style.height = 'auto'
      const height = getComputedStyle(el).height
      el.style.height = '0'
      setTimeout(() => {
        el.style.height = height
      })
    },
    afterEnter(el) {
      el.style.height = 'auto'
    },
    leave(el) {
      el.style.height = getComputedStyle(el).height
      setTimeout(() => {
        el.style.height = '0'
      })
    }
  }
}
</script>

<style>
.expand-enter-active, .expand-leave-active {
  transition: height 0.3s ease;
  overflow: hidden;
}
</style>

注意事项

  • 使用max-height过渡时需设置足够大的值确保内容完全展开
  • 动态高度计算可能引发布局抖动,建议添加overflow: hidden
  • 复杂场景考虑使用requestAnimationFrame优化性能
  • 移动端注意添加-webkit-overflow-scrolling: touch保持滚动流畅

标签: 伸缩vue
分享给朋友:

相关文章

vue实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现登出

vue实现登出

实现Vue登出功能 登出功能通常涉及清除用户会话、令牌或本地存储的数据,并重定向到登录页面。以下是几种常见的实现方式: 清除用户令牌和状态 在Vuex的store中定义一个logout mutati…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现pc

vue实现pc

Vue 实现 PC 端应用开发 Vue.js 是一个流行的前端框架,适用于构建 PC 端 Web 应用。以下是关键步骤和最佳实践: 项目初始化 使用 Vue CLI 或 Vite 创建项目:…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…