当前位置:首页 > VUE

vue实现子弹循环

2026-03-28 21:24:41VUE

Vue 实现子弹循环动画

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

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

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

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: 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 提供更强大的动画控制能力,适合复杂动画场景。

vue实现子弹循环

<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);
}

响应式适配

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

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 cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现尺子

vue实现尺子

Vue 实现尺子组件 实现一个尺子组件可以通过 Canvas 或纯 CSS 完成。以下是两种常见实现方式: 使用 Canvas 绘制尺子 通过 Canvas 动态绘制刻度线和数字,适合需要高精度或动…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…

vue实现登录拦截

vue实现登录拦截

实现登录拦截的方法 在Vue项目中,可以通过路由守卫(Navigation Guards)实现登录拦截,确保未登录用户无法访问受保护的路由。以下是具体实现方式: 使用全局前置守卫 在路由配置文件中(…

vue实现点击跳转

vue实现点击跳转

Vue 实现点击跳转的方法 在 Vue 中实现点击跳转可以通过以下几种方式: 使用 router-link router-link 是 Vue Router 提供的组件,用于声明式导航。适合在模板中…