当前位置:首页 > VUE

vue实现链式运动

2026-01-19 00:06:35VUE

实现链式运动的思路

链式运动指的是多个动画按顺序依次执行,通常需要在前一个动画完成后触发下一个动画。Vue中可以通过结合CSS过渡、JavaScript动画库或自定义逻辑实现。

使用Vue过渡 + CSS实现

通过Vue的<transition>组件和CSS过渡类,结合@after-enter事件实现链式触发:

vue实现链式运动

<template>
  <div>
    <transition @after-enter="handleFirstComplete">
      <div v-if="showFirst" class="box">第一个元素</div>
    </transition>

    <transition @after-enter="handleSecondComplete">
      <div v-if="showSecond" class="box">第二个元素</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showFirst: true,
      showSecond: false
    }
  },
  methods: {
    handleFirstComplete() {
      this.showSecond = true
    },
    handleSecondComplete() {
      console.log('所有动画完成')
    }
  }
}
</script>

<style>
.box {
  width: 100px;
  height: 100px;
  background: red;
  transition: all 1s;
}

.v-enter-active, .v-leave-active {
  transition: opacity 1s, transform 1s;
}
.v-enter, .v-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
</style>

使用JavaScript动画库(GSAP)

GSAP的Timeline功能天然支持链式动画:

vue实现链式运动

<template>
  <div>
    <div ref="box1" class="box">Box 1</div>
    <div ref="box2" class="box">Box 2</div>
  </div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  mounted() {
    const tl = gsap.timeline()
    tl.to(this.$refs.box1, { x: 100, duration: 1 })
      .to(this.$refs.box2, { y: 50, duration: 0.5 })
  }
}
</script>

自定义Promise实现链式

通过Promise封装动画,使用async/await实现顺序执行:

<template>
  <div>
    <div ref="box" class="box" :style="{ left: position + 'px' }"></div>
    <button @click="startChain">开始动画</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      position: 0
    }
  },
  methods: {
    animate(distance, duration) {
      return new Promise(resolve => {
        const start = Date.now()
        const animateFrame = () => {
          const elapsed = Date.now() - start
          const progress = Math.min(elapsed / duration, 1)
          this.position = distance * progress
          if (progress < 1) {
            requestAnimationFrame(animateFrame)
          } else {
            resolve()
          }
        }
        animateFrame()
      })
    },
    async startChain() {
      await this.animate(100, 1000)
      await this.animate(200, 500)
      await this.animate(50, 800)
    }
  }
}
</script>

使用Vue的watch实现

通过观察数据变化触发连续动画:

data() {
  return {
    step: 1,
    box1Style: {},
    box2Style: {}
  }
},
watch: {
  step(newVal) {
    if (newVal === 1) {
      this.animateBox1()
    } else if (newVal === 2) {
      this.animateBox2()
    }
  }
},
methods: {
  animateBox1() {
    // 动画逻辑
    setTimeout(() => {
      this.step = 2
    }, 1000)
  },
  animateBox2() {
    // 动画逻辑
  }
}

每种方法适用于不同场景,CSS过渡适合简单效果,GSAP适合复杂动画序列,Promise方式提供更灵活的控制,watch方式适合与数据驱动紧密结合的场景。

标签: 链式vue
分享给朋友:

相关文章

vue实现下载暂停

vue实现下载暂停

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

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template&g…

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…