当前位置:首页 > VUE

vue实现子弹循环

2026-03-28 21:24:41VUE

Vue 实现子弹循环动画

在 Vue 中实现子弹循环动画可以通过 CSS 动画或 JavaScript 动态控制实现。以下是两种常见方法:

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

通过 Vue 绑定样式,利用 CSS 的 @keyframes 实现子弹移动效果。

<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: Array(5).fill().map(() => ({
        x: Math.random() * 300,
        y: Math.random() * 300
      }))
    };
  },
  mounted() {
    setInterval(() => {
      this.bullets = this.bullets.map(bullet => ({
        x: (bullet.x + 5) % window.innerWidth,
        y: bullet.y
      }));
    }, 50);
  }
};
</script>

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

方法二:使用 GSAP 动画库

GSAP 提供更强大的动画控制能力,适合复杂动画场景。

<template>
  <div ref="container" class="bullet-container">
    <div 
      v-for="(bullet, index) in bullets" 
      :key="index" 
      ref="bullets" 
      class="bullet"
    ></div>
  </div>
</template>

<script>
import { gsap } from 'gsap';

export default {
  data() {
    return {
      bullets: Array(5).fill().map(() => ({
        x: Math.random() * 300,
        y: Math.random() * 300
      }))
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.bullets.forEach((bullet, index) => {
        gsap.to(bullet, {
          x: window.innerWidth,
          duration: 2,
          repeat: -1,
          yoyo: true,
          ease: "none"
        });
      });
    });
  }
};
</script>

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

性能优化建议

对于大量子弹实例,考虑使用 CSS transform 而非直接修改 top/left 属性,因为前者会触发硬件加速。

.bullet {
  transform: translate3d(var(--x), var(--y), 0);
  will-change: transform;
}

实现无限循环的技巧

通过监听子弹移出视口边界时重置位置,可以创建无缝循环效果:

updateBullets() {
  this.bullets = this.bullets.map(bullet => {
    const newX = bullet.x + 5;
    return {
      x: newX > window.innerWidth ? 0 : newX,
      y: bullet.y
    };
  });
  requestAnimationFrame(this.updateBullets);
}

响应式适配

添加窗口大小变化的监听,动态调整子弹初始位置:

vue实现子弹循环

created() {
  window.addEventListener('resize', this.initBullets);
},
beforeDestroy() {
  window.removeEventListener('resize', this.initBullets);
},
methods: {
  initBullets() {
    this.bullets = Array(5).fill().map(() => ({
      x: Math.random() * window.innerWidth,
      y: Math.random() * window.innerHeight
    }));
  }
}

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

相关文章

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现选区

vue实现选区

Vue 实现选区的基本方法 在Vue中实现选区功能通常涉及DOM操作和事件处理。以下是几种常见的方法: 使用原生JavaScript的Selection API 通过window.getSelec…

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…