当前位置:首页 > VUE

vue实现展开动画

2026-02-22 10:38:31VUE

使用 Vue 的 Transition 组件实现展开动画

Vue 提供了内置的 Transition 组件,可以方便地实现元素的展开和折叠动画。以下是一个基本示例:

vue实现展开动画

<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <Transition name="expand">
      <div v-if="isExpanded" class="content">
        This is the content that will expand and collapse
      </div>
    </Transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  }
}
</script>

<style>
.expand-enter-active,
.expand-leave-active {
  transition: height 0.5s ease;
  overflow: hidden;
}

.expand-enter-from,
.expand-leave-to {
  height: 0;
}

.expand-enter-to,
.expand-leave-from {
  height: 100px; /* 设置为内容实际高度或使用auto */
}
</style>

动态高度展开动画

当内容高度不确定时,可以使用 JavaScript 计算高度:

vue实现展开动画

<template>
  <div>
    <button @click="toggle">Toggle</button>
    <Transition
      @before-enter="beforeEnter"
      @enter="enter"
      @after-enter="afterEnter"
      @before-leave="beforeLeave"
      @leave="leave"
    >
      <div v-if="isExpanded" class="content" ref="content">
        Dynamic content height<br>
        More lines<br>
        Even more lines
      </div>
    </Transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggle() {
      this.isExpanded = !this.isExpanded
    },
    beforeEnter(el) {
      el.style.height = '0'
    },
    enter(el) {
      el.style.height = `${el.scrollHeight}px`
    },
    afterEnter(el) {
      el.style.height = 'auto'
    },
    beforeLeave(el) {
      el.style.height = `${el.scrollHeight}px`
    },
    leave(el) {
      setTimeout(() => {
        el.style.height = '0'
      })
    }
  }
}
</script>

<style>
.content {
  transition: height 0.5s ease;
  overflow: hidden;
}
</style>

使用 CSS Grid 实现展开动画

CSS Grid 也可以实现平滑的展开效果:

<template>
  <div class="grid-container">
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <div class="grid-item" :class="{ expanded: isExpanded }">
      Content that expands
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isExpanded: false
    }
  }
}
</script>

<style>
.grid-container {
  display: grid;
  grid-template-rows: auto 0fr;
  transition: grid-template-rows 0.5s ease;
}

.grid-item {
  overflow: hidden;
}

.expanded {
  grid-template-rows: auto 1fr;
}
</style>

使用第三方库(如 VueUse)

VueUse 提供了 useTransition 组合式 API 来实现更复杂的动画:

<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <div :style="style">
      Content with VueUse transition
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useTransition } from '@vueuse/core'

export default {
  setup() {
    const isExpanded = ref(false)
    const height = useTransition(isExpanded, {
      duration: 500,
      transition: [0.75, 0, 0.25, 1]
    })

    const style = computed(() => ({
      height: `${height.value * 100}px`,
      overflow: 'hidden'
    }))

    return { isExpanded, style }
  }
}
</script>

注意事项

  • 确保动画元素有 overflow: hidden 属性
  • 对于动态内容,需要在动画前获取元素的实际高度
  • 使用 requestAnimationFrame 可以优化动画性能
  • 考虑添加 will-change: height 属性提升动画性能
  • 移动端设备可能需要添加 -webkit-overflow-scrolling: touch

标签: 动画vue
分享给朋友:

相关文章

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.io…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue源码实现

vue源码实现

Vue 源码实现解析 Vue.js 的核心实现可以分为响应式系统、虚拟 DOM、模板编译、组件化等几个关键部分。以下是对这些核心机制的详细解析。 响应式系统 Vue 的响应式系统基于 Object.…