当前位置:首页 > VUE

vue实现小球教学

2026-01-18 17:30:05VUE

Vue 实现小球动画基础

在 Vue 中实现小球动画可以通过 CSS 过渡、JavaScript 动画库或结合两者完成。以下是几种常见方法:

使用 CSS Transition 通过 Vue 的 v-bind:stylev-bind:class 动态修改小球的样式属性(如 left/top),配合 CSS 的 transition 实现平滑移动:

vue实现小球教学

<template>
  <div class="ball" :style="{ left: x + 'px', top: y + 'px' }"></div>
  <button @click="moveBall">移动小球</button>
</template>

<script>
export default {
  data() {
    return { x: 0, y: 0 };
  },
  methods: {
    moveBall() {
      this.x += 50;
      this.y += 30;
    }
  }
};
</script>

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

使用 JavaScript 动画库(如 GSAP) GSAP 提供更复杂的动画控制,适合需要弹性效果或路径运动的场景:

vue实现小球教学

<template>
  <div ref="ball" class="ball"></div>
  <button @click="animateBall">弹性动画</button>
</template>

<script>
import { gsap } from 'gsap';
export default {
  methods: {
    animateBall() {
      gsap.to(this.$refs.ball, {
        x: 200,
        y: 100,
        duration: 1,
        ease: "bounce.out"
      });
    }
  }
};
</script>

实现物理效果(重力与碰撞)

通过 JavaScript 计算小球位置并实时更新 DOM,模拟物理效果:

<template>
  <div class="container" @click="dropBall">
    <div class="ball" :style="ballStyle"></div>
  </div>
</template>

<script>
export default {
  data() {
    return { y: 0, velocity: 0, gravity: 0.5 };
  },
  computed: {
    ballStyle() {
      return { transform: `translateY(${this.y}px)` };
    }
  },
  methods: {
    dropBall() {
      this.y = 0;
      this.velocity = 0;
      const animate = () => {
        this.velocity += this.gravity;
        this.y += this.velocity;
        if (this.y < 300) requestAnimationFrame(animate);
      };
      animate();
    }
  }
};
</script>

<style>
.container {
  height: 300px;
  border: 1px solid #ccc;
  position: relative;
}
.ball {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: blue;
}
</style>

拖拽交互实现

结合 Vue 的指令和原生 DOM 事件实现拖拽功能:

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

<script>
export default {
  data() {
    return { 
      pos: { x: 100, y: 100 },
      isDragging: false,
      offset: { x: 0, y: 0 }
    };
  },
  mounted() {
    document.addEventListener('mousemove', this.drag);
    document.addEventListener('mouseup', this.stopDrag);
  },
  methods: {
    startDrag(e) {
      this.isDragging = true;
      this.offset.x = e.clientX - this.pos.x;
      this.offset.y = e.clientY - this.pos.y;
    },
    drag(e) {
      if (!this.isDragging) return;
      this.pos.x = e.clientX - this.offset.x;
      this.pos.y = e.clientY - this.offset.y;
    },
    stopDrag() {
      this.isDragging = false;
    }
  }
};
</script>

性能优化建议

  • 使用 transform 替代 top/left 属性,触发 GPU 加速
  • 对频繁更新的动画使用 requestAnimationFrame
  • 避免在 v-for 中为每个小球创建独立监听器,改用事件委托
  • 复杂场景考虑使用 Canvas 或 WebGL 渲染(如 Three.js)

以上方法可根据实际需求组合使用,例如用 GSAP 处理动画,Vue 管理状态,CSS 处理基础样式。

标签: 小球vue
分享给朋友:

相关文章

vue实现共享屏幕

vue实现共享屏幕

共享屏幕的基本实现 在Vue中实现共享屏幕功能通常需要结合浏览器的getDisplayMedia API。以下是一个基础实现示例: <template> <div>…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…