当前位置:首页 > 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 动画适合简单路径。

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

相关文章

css制作小球动画

css制作小球动画

使用 CSS 制作小球动画 关键帧动画(@keyframes) 通过 @keyframes 定义小球的运动轨迹,结合 transform 和 animation 属性实现动画效果。 .ball {…

css制作红色小球

css制作红色小球

使用CSS制作红色小球 方法1:使用纯CSS 通过border-radius属性设置为50%创建圆形,并设置背景色为红色。 .red-ball { width: 50px; height:…

vue实现小球弹入

vue实现小球弹入

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

vue实现小球教学

vue实现小球教学

Vue实现小球动画的基础方法 使用Vue实现小球动画可以通过结合CSS过渡和JavaScript动画库。以下是一个基础实现示例: 创建Vue组件模板部分 <template> &l…

vue实现小球移动

vue实现小球移动

Vue实现小球移动的方法 使用CSS动画和Vue数据绑定 在Vue中可以通过结合CSS动画和数据绑定实现小球移动效果。定义一个包含小球样式的元素,通过修改其样式属性实现移动。 <templat…

vue 2.0实现小球

vue 2.0实现小球

Vue 2.0 实现小球动画 在 Vue 2.0 中实现小球动画可以通过结合 CSS 动画和 Vue 的动态绑定特性完成。以下是一个完整的实现方案,包含模板、样式和逻辑代码。 模板部分 <te…