当前位置:首页 > 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状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现下载暂停

vue实现下载暂停

Vue实现下载暂停功能 在Vue中实现下载暂停功能,通常需要结合XMLHttpRequest或Fetch API的AbortController来控制请求中断。以下是具体实现方法: 使用XMLHtt…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null } },…