当前位置:首页 > VUE

vue实现小球自由移动

2026-02-24 12:01:57VUE

Vue 实现小球自由移动

在 Vue 中实现小球自由移动可以通过动态绑定样式、使用 requestAnimationFrame 或 CSS 动画来实现。以下是几种常见的方法:

方法一:动态绑定样式 + JavaScript 动画

通过 Vue 的 datamethods 动态更新小球的 lefttop 值,结合 requestAnimationFrame 实现平滑动画。

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

<script>
export default {
  data() {
    return {
      x: 0,
      y: 0,
      dx: 2,
      dy: 2,
      width: 800,
      height: 600,
    };
  },
  mounted() {
    this.animate();
  },
  methods: {
    animate() {
      this.x += this.dx;
      this.y += this.dy;

      // 边界检测
      if (this.x <= 0 || this.x >= this.width - 30) {
        this.dx = -this.dx;
      }
      if (this.y <= 0 || this.y >= this.height - 30) {
        this.dy = -this.dy;
      }

      requestAnimationFrame(this.animate);
    },
  },
};
</script>

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

方法二:CSS 动画 + @keyframes

通过 CSS @keyframes 定义动画路径,利用 Vue 动态绑定类名或样式。

<template>
  <div class="ball" :style="ballStyle"></div>
</template>

<script>
export default {
  data() {
    return {
      duration: 5, // 动画时长(秒)
    };
  },
  computed: {
    ballStyle() {
      return {
        animation: `move ${this.duration}s linear infinite`,
      };
    },
  },
};
</script>

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

@keyframes move {
  0% {
    transform: translate(0, 0);
  }
  25% {
    transform: translate(200px, 100px);
  }
  50% {
    transform: translate(400px, 0);
  }
  75% {
    transform: translate(200px, -100px);
  }
  100% {
    transform: translate(0, 0);
  }
}
</style>

方法三:使用 Vue Transition

结合 Vue 的 <transition> 和动态样式实现更复杂的交互效果。

vue实现小球自由移动

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

<script>
export default {
  data() {
    return {
      x: 0,
      y: 0,
    };
  },
  mounted() {
    setInterval(() => {
      this.x = Math.random() * 300;
      this.y = Math.random() * 300;
    }, 1000);
  },
};
</script>

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

.slide-enter-active, .slide-leave-active {
  transition: all 0.5s;
}
</style>

关键点总结

  • 动态绑定样式:通过 Vue 的 :style 绑定小球的 lefttop 实现位置变化。
  • 动画控制:使用 requestAnimationFramesetInterval 更新坐标。
  • 边界检测:在 JavaScript 中检查小球是否超出边界并反转方向。
  • CSS 动画:通过 @keyframestransition 实现平滑效果。

根据需求选择合适的方法,动态绑定样式适合复杂逻辑,CSS 动画适合简单路径。

标签: 小球自由
分享给朋友:

相关文章

vue实现财务自由

vue实现财务自由

Vue 实现财务自由的方法 通过 Vue.js 技术实现财务自由,需要结合技术变现、职业发展、副业拓展等多种途径。以下是一些可行的方向: 开发 Vue 相关产品 利用 Vue.js 开发商业级应用或…

css制作小球

css制作小球

CSS 制作小球的方法 使用 CSS 可以轻松创建各种样式的小球,以下是几种常见的方法: 使用 border-radius 制作圆形 通过将元素的 border-radius 设置为 50%,可以将…

vue实现自由拖拽

vue实现自由拖拽

Vue 实现自由拖拽的方法 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性结合 Vue 的事件监听实现拖拽功能。以下是一个简单的实现示例:…

vue 实现财务自由

vue 实现财务自由

Vue 实现财务自由的途径 Vue.js 作为前端框架,本身无法直接带来财务自由,但可以通过以下方式利用 Vue 技术创造收入或提升职业发展潜力: 开发高需求产品 利用 Vue 构建 SaaS 产品…

vue实现小球弹入

vue实现小球弹入

Vue 实现小球弹入动画 使用 Vue 实现小球弹入动画可以通过 CSS 动画或 JavaScript 动画库(如 GSAP)来完成。以下是两种常见的方法: 使用 CSS 过渡和动画 在 V…

vue 实现自由切图

vue 实现自由切图

实现自由切图的基本思路 在Vue中实现自由切图功能,可以通过结合HTML5的Canvas或SVG技术,以及第三方库如Fabric.js或Konva.js来实现。这些库提供了丰富的API,便于处理图像裁…