当前位置:首页 > 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 类名和过渡属性控制伸缩效果。

vue实现伸缩框

<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 快速实现伸缩功能。

vue实现伸缩框

安装:

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实现图片轮播

使用 Swiper 实现图片轮播 安装 Swiper 依赖 npm install swiper 在 Vue 组件中引入 Swiper <template> <div c…

vue实现分支

vue实现分支

Vue 实现分支的方法 在 Vue 项目中实现分支功能通常涉及条件渲染、动态组件或路由控制。以下是几种常见的实现方式: 条件渲染(v-if/v-show) 使用 Vue 的指令根据条件显示不同内容:…

vue怎么实现页面返回

vue怎么实现页面返回

Vue 实现页面返回的方法 在 Vue 中实现页面返回功能,可以通过以下几种方式完成,具体取决于项目使用的路由模式和技术栈。 使用 Vue Router 的编程式导航 通过 this.$route…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPag…

vue实现本地数据存储

vue实现本地数据存储

使用 localStorage 或 sessionStorage Vue 中可以直接调用浏览器提供的 localStorage 或 sessionStorage API 进行数据存储。localSto…

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue…