当前位置:首页 > VUE

vue实现链式运动

2026-01-19 00:06:35VUE

实现链式运动的思路

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

使用Vue过渡 + CSS实现

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

<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功能天然支持链式动画:

<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实现

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

vue实现链式运动

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.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例:…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue实现批量

vue实现批量

Vue 实现批量操作的方法 在 Vue 中实现批量操作通常涉及选择多个项目并执行统一处理,例如批量删除、批量更新等。以下是几种常见实现方式: 表格多选批量操作 使用 el-table 配合复选框实现…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…