当前位置:首页 > 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 Router 的编程式导航 通过 this.$router…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放 A…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue实现token

vue实现token

Vue 中实现 Token 认证的方法 在 Vue 中实现 Token 认证通常涉及前端与后端的交互,包括 Token 的获取、存储、发送和刷新等操作。以下是具体实现步骤: 获取 Token 用户登…

vue实现flvvideo

vue实现flvvideo

vue-flv-player 实现 FLV 视频播放 安装依赖包 npm install flv.js vue-flv-player 基础组件引入 <template> <v…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…