当前位置:首页 > VUE

vue实现点击弧线动画

2026-01-21 04:57:13VUE

实现点击弧线动画的方法

在Vue中实现点击弧线动画可以通过CSS和JavaScript结合的方式完成。以下是具体实现步骤:

使用CSS和Vue事件绑定

通过Vue的@click事件触发动画,结合CSS的transitiontransform属性实现弧线效果。

<template>
  <div class="container">
    <div 
      class="ball" 
      @click="animate"
      :style="{ transform: `translate(${x}px, ${y}px)` }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      x: 0,
      y: 0,
      isAnimating: false
    }
  },
  methods: {
    animate() {
      if (this.isAnimating) return;
      this.isAnimating = true;

      const startX = 0;
      const startY = 0;
      const endX = 200;
      const endY = 100;
      const controlX = 100;
      const controlY = -50;

      const duration = 1000;
      const startTime = performance.now();

      const animateFrame = (currentTime) => {
        const elapsed = currentTime - startTime;
        const progress = Math.min(elapsed / duration, 1);

        // 贝塞尔曲线计算
        this.x = (1 - progress)  2 * startX + 
                 2 * (1 - progress) * progress * controlX + 
                 progress  2 * endX;
        this.y = (1 - progress)  2 * startY + 
                 2 * (1 - progress) * progress * controlY + 
                 progress  2 * endY;

        if (progress < 1) {
          requestAnimationFrame(animateFrame);
        } else {
          this.isAnimating = false;
        }
      };

      requestAnimationFrame(animateFrame);
    }
  }
}
</script>

<style>
.ball {
  width: 30px;
  height: 30px;
  background-color: #42b983;
  border-radius: 50%;
  transition: transform 0.1s linear;
}
.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ddd;
}
</style>

使用GSAP库实现

GSAP是一个强大的动画库,可以更简单地实现复杂的弧线动画。

<template>
  <div class="container">
    <div ref="ball" class="ball" @click="animate"></div>
  </div>
</template>

<script>
import { gsap } from 'gsap';

export default {
  methods: {
    animate() {
      gsap.to(this.$refs.ball, {
        duration: 1,
        x: 200,
        y: 100,
        ease: "sine.inOut",
        motionPath: {
          path: [{ x: 0, y: 0 }, { x: 100, y: -50 }, { x: 200, y: 100 }],
          type: "cubic"
        }
      });
    }
  }
}
</script>

<style>
.ball {
  width: 30px;
  height: 30px;
  background-color: #42b983;
  border-radius: 50%;
}
.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ddd;
}
</style>

使用CSS关键帧动画

通过CSS的@keyframes定义弧线路径,Vue控制动画的触发。

vue实现点击弧线动画

<template>
  <div class="container">
    <div 
      class="ball" 
      @click="startAnimation"
      :class="{ 'animate': isAnimating }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isAnimating: false
    }
  },
  methods: {
    startAnimation() {
      this.isAnimating = true;
      setTimeout(() => {
        this.isAnimating = false;
      }, 1000);
    }
  }
}
</script>

<style>
.ball {
  width: 30px;
  height: 30px;
  background-color: #42b983;
  border-radius: 50%;
  position: relative;
}
.animate {
  animation: arcAnimation 1s ease-in-out;
}
@keyframes arcAnimation {
  0% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(100px, -50px);
  }
  100% {
    transform: translate(200px, 100px);
  }
}
.container {
  width: 300px;
  height: 200px;
  border: 1px solid #ddd;
}
</style>

选择适合的方法

  • 纯CSS和JavaScript:适合简单的动画,无需额外依赖。
  • GSAP:适合复杂的动画效果,提供更多控制选项。
  • CSS关键帧:适合性能要求高的场景,但灵活性较低。

根据项目需求选择最适合的实现方式。

标签: 弧线动画
分享给朋友:

相关文章

vue实现动画

vue实现动画

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要通过内置组件和 CSS 过渡/动画实现。 使用 <transition> 组件 Vue 的 <transition&g…

vue实现动态弧线

vue实现动态弧线

Vue 实现动态弧线的方法 动态弧线可以通过 SVG 或 Canvas 实现,以下是两种常见的方法: 使用 SVG 实现动态弧线 SVG 的 <path> 元素可以绘制弧线,结合 Vue…

vue实现过渡动画

vue实现过渡动画

Vue 过渡动画实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于处理进入/离开过渡和列表动画。以下是几种常见的实现…

不用jquery实现vue动画

不用jquery实现vue动画

使用纯JavaScript实现Vue动画 在Vue中实现动画效果可以不依赖jQuery,通过Vue的内置过渡系统或直接操作CSS和JavaScript即可完成。以下是几种常见方法: 使用Vue的&l…

vue不点击实现动画

vue不点击实现动画

在Vue中实现不依赖点击触发的动画,可以通过多种方式实现,例如利用生命周期钩子、条件渲染、CSS动画或第三方库。以下是几种常见方法: 使用Vue的过渡和动画系统 Vue内置的<transiti…

vue实现切换路由动画

vue实现切换路由动画

路由动画的基本实现 在Vue中实现路由切换动画,通常结合<transition>或<transition-group>组件与Vue Router完成。以下是基础实现方式: &…