当前位置:首页 > 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实现检测数组

vue实现检测数组

Vue 中检测数组变化的方法 Vue 的响应式系统无法直接检测到数组的某些变化,因为 JavaScript 的限制。以下是一些解决方案: 使用 Vue.set 或 this.$set Vue 提供…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return {…

vue实现a

vue实现a

Vue 实现 A 功能的方法 在 Vue 中实现 A 功能可以通过多种方式,具体取决于 A 功能的具体需求。以下是几种常见的实现方法: 使用 Vue 组件 通过创建自定义组件来实现 A 功能。组件…

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…