当前位置:首页 > VUE

vue实现伸缩框

2026-01-19 21:39:07VUE

Vue 实现伸缩框的方法

使用 CSS Transition 和 v-show/v-if

通过 Vue 的指令结合 CSS 过渡效果实现伸缩动画。定义一个布尔值控制元素的显示隐藏,并添加 CSS 过渡属性。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div class="expandable" :class="{ 'expanded': isExpanded }">
      Content here...
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style>
.expandable {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}
.expandable.expanded {
  max-height: 500px; /* Adjust based on content */
}
</style>

使用 Vue Transition 组件

Vue 内置的 <transition> 组件可以更方便地实现动画效果。通过设置 CSS 类名和过渡属性控制伸缩效果。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <transition name="slide">
      <div v-show="isExpanded" class="content">
        Content here...
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style>
.slide-enter-active, .slide-leave-active {
  transition: max-height 0.3s ease;
}
.slide-enter, .slide-leave-to {
  max-height: 0;
  overflow: hidden;
}
.slide-enter-to, .slide-leave {
  max-height: 500px;
}
</style>

动态计算高度

如果内容高度不确定,可以通过 JavaScript 动态计算内容高度并应用动画。

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <div class="expandable" :style="{ height: height }">
      <div ref="content">
        Content here...
      </div>
    </div>
  </div>
</template>

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

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

使用第三方库(如 Vue-Collapse)

对于更复杂的需求,可以使用第三方库如 vue-collapse 快速实现伸缩功能。

安装:

npm install vue-collapse

使用:

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <vue-collapse :show="isExpanded">
      <div class="content">
        Content here...
      </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>

以上方法可以根据具体需求选择,从简单的 CSS 过渡到动态高度计算或第三方库,灵活实现伸缩框效果。

vue实现伸缩框

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

相关文章

vue实现多选div

vue实现多选div

实现多选 DIV 的基本思路 在 Vue 中实现多选 DIV 功能,可以通过以下方法实现。主要思路是利用 v-model 绑定选中状态,结合 CSS 类名动态切换样式。 数据准备与模板结构 定义一…

vue实现滚动截屏

vue实现滚动截屏

实现滚动截屏的基本思路 滚动截屏通常指对超出可视区域的长页面进行完整截图。在Vue中,可以通过结合DOM操作、Canvas绘制及第三方库实现。以下是具体方法: 使用html2canvas库实现 安装…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul…

vue实现贴图

vue实现贴图

Vue 实现贴图功能 在Vue中实现贴图功能通常涉及将图片动态添加到指定位置或元素上。以下是几种常见实现方式: 使用v-bind动态绑定图片 通过数据驱动的方式动态更换图片源: <tem…

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue实现跑车

vue实现跑车

Vue 实现跑车动画效果 使用 Vue 结合 CSS 动画可以实现跑车在屏幕上移动的效果。以下是一个完整的实现方案: 创建 Vue 组件结构 <template> <div c…