当前位置:首页 > VUE

vue实现子弹循环

2026-03-08 10:33:17VUE

实现子弹循环动画的基本思路

在Vue中实现子弹循环动画,可以通过CSS动画或JavaScript定时器控制元素的位置变化。以下提供两种常见实现方式:

方法一:CSS动画结合Vue动态样式

<template>
  <div class="bullet-container">
    <div 
      v-for="(bullet, index) in bullets" 
      :key="index" 
      class="bullet" 
      :style="{ left: bullet.x + 'px', top: bullet.y + 'px' }"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bullets: [],
      containerWidth: 800,
      containerHeight: 400
    }
  },
  mounted() {
    this.startBulletLoop()
  },
  methods: {
    createBullet() {
      return {
        x: Math.random() * this.containerWidth,
        y: Math.random() * this.containerHeight,
        speedX: (Math.random() - 0.5) * 10,
        speedY: (Math.random() - 0.5) * 10
      }
    },
    updateBullets() {
      this.bullets = this.bullets.map(bullet => {
        let newX = bullet.x + bullet.speedX
        let newY = bullet.y + bullet.speedY

        // 边界检测,实现循环
        if (newX > this.containerWidth) newX = 0
        if (newX < 0) newX = this.containerWidth
        if (newY > this.containerHeight) newY = 0
        if (newY < 0) newY = this.containerHeight

        return { ...bullet, x: newX, y: newY }
      })
    },
    startBulletLoop() {
      // 初始化子弹
      for (let i = 0; i < 20; i++) {
        this.bullets.push(this.createBullet())
      }

      // 启动动画循环
      setInterval(() => {
        this.updateBullets()
      }, 50)
    }
  }
}
</script>

<style>
.bullet-container {
  position: relative;
  width: 800px;
  height: 400px;
  border: 1px solid #ccc;
  overflow: hidden;
}

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

方法二:使用CSS关键帧动画实现循环

<template>
  <div class="bullet-track">
    <div class="bullet" v-for="n in 5" :key="n"></div>
  </div>
</template>

<style>
.bullet-track {
  position: relative;
  width: 500px;
  height: 50px;
  border: 1px solid #eee;
  overflow: hidden;
}

.bullet {
  position: absolute;
  width: 20px;
  height: 20px;
  background-color: blue;
  border-radius: 50%;
  animation: bulletMove 3s linear infinite;
}

@keyframes bulletMove {
  0% {
    transform: translateX(-30px);
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    transform: translateX(530px);
    opacity: 0;
  }
}

.bullet:nth-child(1) { animation-delay: 0s; top: 5px; }
.bullet:nth-child(2) { animation-delay: 0.5s; top: 15px; }
.bullet:nth-child(3) { animation-delay: 1s; top: 25px; }
.bullet:nth-child(4) { animation-delay: 1.5s; top: 15px; }
.bullet:nth-child(5) { animation-delay: 2s; top: 5px; }
</style>

性能优化建议

  • 对于大量子弹元素,考虑使用Canvas或WebGL渲染
  • 使用requestAnimationFrame代替setInterval可获得更流畅的动画
  • 对于Vue的响应式数据,避免频繁更新大型数组
  • 使用CSS transform属性进行动画比修改top/left性能更好

复杂场景扩展

需要实现更复杂的子弹轨迹时,可以:

vue实现子弹循环

  • 预定义不同的运动路径函数(直线、曲线、螺旋等)
  • 使用贝塞尔曲线控制运动轨迹
  • 添加碰撞检测逻辑
  • 实现子弹发射源和角度控制

标签: 子弹vue
分享给朋友:

相关文章

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会自…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…