当前位置:首页 > 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实现多选div

vue实现多选div

实现多选 DIV 的基本思路 在 Vue 中实现多选 DIV 功能,可以通过以下方法实现。主要思路是利用 v-model 绑定选中状态,结合 CSS 类名动态切换样式。 数据准备与模板结构 定义一个…

vue实现点击页面切换

vue实现点击页面切换

实现点击页面切换功能 在Vue中实现点击页面切换功能,可以通过路由跳转或组件动态渲染两种方式完成。以下是具体实现方法: 使用Vue Router实现页面跳转 安装Vue Router后,在项目中配置…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="clock-…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sel…

vue实现选区

vue实现选区

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

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…