当前位置:首页 > VUE

vue 实现抛物线

2026-02-24 20:00:01VUE

Vue 实现抛物线动画

在 Vue 中实现抛物线动画,可以通过 CSS 动画或 JavaScript 动态计算位置来实现。以下是两种常见方法:

方法一:使用 CSS animation@keyframes

通过 CSS 定义抛物线关键帧动画,适用于简单的抛物线效果。

<template>
  <div class="ball" :style="{ left: x + 'px', top: y + 'px' }"></div>
</template>

<script>
export default {
  data() {
    return {
      x: 0,
      y: 0
    };
  },
  mounted() {
    this.startAnimation();
  },
  methods: {
    startAnimation() {
      const duration = 2000; // 动画时长(毫秒)
      const startTime = Date.now();

      const animate = () => {
        const elapsed = Date.now() - startTime;
        const progress = Math.min(elapsed / duration, 1);

        // 抛物线公式:y = a * x^2 + b * x + c
        this.x = progress * 300; // 水平位移
        this.y = 200 * Math.pow(progress, 2) - 100 * progress; // 垂直位移

        if (progress < 1) {
          requestAnimationFrame(animate);
        }
      };

      animate();
    }
  }
};
</script>

<style>
.ball {
  position: absolute;
  width: 20px;
  height: 20px;
  background-color: red;
  border-radius: 50%;
  transition: all 0.1s linear;
}
</style>

方法二:使用 GSAP 动画库

GSAP 提供了更强大的动画控制能力,适合复杂的抛物线效果。

<template>
  <div ref="ball" class="ball"></div>
</template>

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

export default {
  mounted() {
    this.startGSAPAnimation();
  },
  methods: {
    startGSAPAnimation() {
      gsap.to(this.$refs.ball, {
        duration: 2,
        x: 300,
        y: 200,
        ease: "power1.inOut"
      });
    }
  }
};
</script>

<style>
.ball {
  position: absolute;
  width: 20px;
  height: 20px;
  background-color: blue;
  border-radius: 50%;
}
</style>

方法三:自定义抛物线函数

通过数学公式动态计算位置,实现更灵活的抛物线效果。

vue 实现抛物线

methods: {
  parabolicMotion(startX, startY, endX, endY, duration) {
    const startTime = Date.now();
    const animate = () => {
      const elapsed = Date.now() - startTime;
      const progress = Math.min(elapsed / duration, 1);

      // 计算当前 x 和 y 坐标
      this.x = startX + (endX - startX) * progress;
      this.y = startY + (endY - startY) * progress - 200 * Math.pow(progress, 2);

      if (progress < 1) {
        requestAnimationFrame(animate);
      }
    };

    animate();
  }
}

注意事项

  • 抛物线公式一般形式为 $y = ax^2 + bx + c$,通过调整系数可以改变抛物线形状。
  • 使用 requestAnimationFrame 实现平滑动画,避免直接修改 DOM。
  • 对于复杂动画,推荐使用 GSAP 或 Anime.js 等专业动画库。

标签: 抛物线vue
分享给朋友:

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue实现点击旋转轮盘

vue实现点击旋转轮盘

实现点击旋转轮盘效果 在Vue中实现点击旋转轮盘效果,可以通过CSS动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue C…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…