当前位置:首页 > VUE

vue实现小球自由移动

2026-02-24 12:01:57VUE

Vue 实现小球自由移动

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

vue实现小球自由移动

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

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

vue实现小球自由移动

<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> 和动态样式实现更复杂的交互效果。

<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 技术实现财务自由,核心在于将技术能力转化为可持续的收入来源。以下是几种常见的方法: 开发高需求 Vue 项目或产品 构建基于 Vue 的 SaaS 工具、企…

vue实现小球教学

vue实现小球教学

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

vue实现小球移动

vue实现小球移动

Vue 实现小球移动的方法 使用 CSS 动画和 Vue 数据绑定 在 Vue 中可以通过数据绑定控制 CSS 属性来实现小球移动效果。定义一个数据属性存储小球位置,使用 v-bind:style 动…

css制作小球

css制作小球

使用CSS制作小球 通过CSS可以轻松创建各种样式的小球效果,以下是几种常见方法: 基础圆形小球 .ball { width: 50px; height: 50px; border-r…

css制作小球

css制作小球

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

css制作小球动画

css制作小球动画

使用CSS制作小球动画 CSS动画可以通过@keyframes和animation属性实现小球动画效果。以下是几种常见的小球动画实现方式: 基础弹跳动画 通过@keyframes定义小球的弹跳轨迹,…