当前位置:首页 > 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实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现banner

vue实现banner

Vue实现Banner轮播 使用Vue实现Banner轮播可以通过第三方库如swiper或vue-awesome-swiper,也可以手动实现基础轮播效果。以下是两种常见方法: 使用vue-awes…

vue实现字幕

vue实现字幕

Vue 实现字幕功能 在 Vue 中实现字幕功能可以通过动态绑定数据和 CSS 动画来实现。以下是具体实现方法: 1. 基础字幕组件 创建一个字幕组件,通过 v-model 或 props 接收字…

vue简单实现

vue简单实现

Vue 简单实现示例 以下是一个使用 Vue.js 创建简单应用的示例,涵盖基本功能如数据绑定、事件处理和组件化。 基础 HTML 结构 <!DOCTYPE html> <html…

vue实现产品搜索

vue实现产品搜索

实现产品搜索功能 在Vue中实现产品搜索功能需要结合前端界面和后端数据处理。以下是实现的基本思路和代码示例: 数据准备 创建一个产品数据数组,包含需要搜索的产品信息: data() { ret…