当前位置:首页 > 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元素的实际高度:

<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组件:

    <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>组件实现更丰富的动画:

vue中实现伸缩框

<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中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <ul…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue  实现分页

vue 实现分页

实现分页的基本思路 在Vue中实现分页通常需要结合后端API返回的数据和前端的分页组件。分页的核心逻辑包括计算总页数、处理当前页码变化以及触发数据请求。 分页组件示例 使用Element UI的分页…

vue下载实现

vue下载实现

Vue 下载实现方法 在 Vue 项目中实现文件下载功能可以通过多种方式完成,以下是几种常见的方法: 使用 <a> 标签实现下载 通过动态创建 <a> 标签并设置 href…