当前位置:首页 > 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控制动画的触发。

<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实现点击弧线动画

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

相关文章

css3动画在线制作

css3动画在线制作

CSS3 动画在线制作工具 以下是一些常用的在线工具和平台,可以帮助快速生成 CSS3 动画代码,无需手动编写: Animista 网址:https://animista.net/ 特点:提供预设的…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要包括内置组件和第三方库。 使用 <transition> 和 <transition-group> Vue 内置…

vue实现ios动画

vue实现ios动画

Vue 实现 iOS 动画的方法 使用 CSS Transition 和 Vue 的过渡系统 Vue 提供了内置的过渡系统,可以通过 <transition> 组件实现动画效果。结合 CS…

vue 实现动画效果

vue 实现动画效果

Vue 实现动画效果的方法 Vue 提供了多种方式实现动画效果,包括内置的过渡系统、第三方动画库以及 CSS 动画。以下是几种常见的方法: 使用 Vue 内置过渡系统 Vue 的 <tran…

vue实现动画插件

vue实现动画插件

Vue 动画插件实现方案 Vue 提供了多种实现动画的方式,可以通过内置的 <transition> 组件或第三方插件来实现复杂的动画效果。 使用 Vue 内置 <transit…

vue中动画实现

vue中动画实现

Vue 中动画的实现方法 Vue 提供了多种方式实现动画效果,主要依赖于内置的 <transition> 和 <transition-group> 组件,以及结合 CSS 或…