当前位置:首页 > VUE

vue实现点击弧线动画

2026-01-21 04:57:13VUE

实现点击弧线动画的方法

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

vue实现点击弧线动画

使用CSS和Vue事件绑定

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

vue实现点击弧线动画

<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哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果,可以通过以下组件和方法实现: Vue内置动画组件 Vue提供了<transition>和<transition-group>两个内置组件,用于处理进入…

vue 动画 实现

vue 动画 实现

Vue 动画实现基础 Vue 提供了 <transition> 和 <transition-group> 组件,结合 CSS 或 JavaScript 钩子实现动画效果。核心是…

vue实现动画

vue实现动画

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

vue 实现动画

vue 实现动画

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,包括内置组件、CSS 过渡/动画以及第三方库集成。 使用 <transition> 组件 Vue 内置的 <transi…

react如何动画

react如何动画

React 动画实现方法 React 中实现动画可以通过多种方式,包括原生 CSS、第三方库或 React 内置的动画解决方案。以下是几种常见方法: 使用 CSS 动画和过渡 通过 CSS 的 t…

jquery 动画

jquery 动画

jQuery 动画基础 jQuery 提供了一系列动画方法,用于实现元素的动态效果,如淡入淡出、滑动、自定义动画等。 常用动画方法 fadeIn() / fadeOut(): 元素的淡…