当前位置:首页 > VUE

vue实现展开动画

2026-01-21 19:25:39VUE

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

Vue 内置的 Transition 组件可以方便地实现元素的展开/收起动画效果。通过添加 CSS 过渡类名,定义进入和离开的动画过程。

vue实现展开动画

<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <Transition name="expand">
      <div v-show="isExpanded" class="content">
        Content to be expanded
      </div>
    </Transition>
  </div>
</template>

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

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

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

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

使用动态高度实现自动计算

当内容高度不确定时,可以使用 JavaScript 动态计算高度,实现更灵活的展开动画。

vue实现展开动画

<template>
  <div>
    <button @click="toggleExpand">Toggle</button>
    <div class="container">
      <div ref="content" class="content" :style="{ height: currentHeight }">
        <p>Dynamic content here...</p>
        <p>Line 2</p>
        <p>Line 3</p>
      </div>
    </div>
  </div>
</template>

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

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

.content {
  padding: 10px;
  background: #f5f5f5;
}
</style>

使用第三方动画库(如 GSAP)

对于更复杂的动画效果,可以集成 GSAP 等专业动画库。

<template>
  <div>
    <button @click="toggleExpand">Toggle</button>
    <div ref="content" class="content">
      <p>Content with GSAP animation</p>
    </div>
  </div>
</template>

<script>
import gsap from 'gsap'

export default {
  data() {
    return {
      isExpanded: false
    }
  },
  methods: {
    toggleExpand() {
      this.isExpanded = !this.isExpanded
      if (this.isExpanded) {
        gsap.to(this.$refs.content, {
          height: 'auto',
          duration: 0.3,
          ease: 'power2.out'
        })
      } else {
        gsap.to(this.$refs.content, {
          height: 0,
          duration: 0.3,
          ease: 'power2.in'
        })
      }
    }
  },
  mounted() {
    gsap.set(this.$refs.content, { height: 0 })
  }
}
</script>

<style>
.content {
  overflow: hidden;
}
</style>

使用 VueUse 的 useTransition 组合式 API

VueUse 库提供了更便捷的动画解决方案。

<template>
  <div>
    <button @click="isExpanded = !isExpanded">Toggle</button>
    <div class="content" :style="{ height: `${height}px` }">
      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 source = ref(0)
    const target = ref(100) // 目标高度

    const height = useTransition(source, {
      duration: 300,
      transition: [0.25, 0.1, 0.25, 1] // CSS easing function
    })

    watch(isExpanded, (val) => {
      source.value = val ? target.value : 0
    })

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

<style>
.content {
  overflow: hidden;
  background: #f5f5f5;
  padding: 10px;
}
</style>

注意事项

  • 确保动画元素设置了 overflow: hidden 属性,防止内容溢出
  • 对于性能优化,考虑使用 will-change: height 属性
  • 在移动设备上,添加 -webkit-overflow-scrolling: touch 改善滚动体验
  • 动画持续时间建议在 200-300ms 之间,以获得最佳用户体验

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

相关文章

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

vue实现例子

vue实现例子

以下是一些常见的 Vue 实现例子,涵盖基础功能到进阶应用场景: 基础数据绑定 使用 v-model 实现双向数据绑定: <template> <div> &l…