当前位置:首页 > VUE

vue实现点击烟花

2026-03-10 07:33:36VUE

Vue 实现点击烟花效果

方法一:使用 CSS 动画和动态元素

在 Vue 模板中添加点击事件监听,动态生成烟花元素并应用 CSS 动画:

<template>
  <div class="fireworks-container" @click="createFirework">
    <!-- 点击区域 -->
  </div>
</template>

<script>
export default {
  methods: {
    createFirework(e) {
      const fireworks = document.createElement('div')
      fireworks.className = 'firework'
      fireworks.style.left = `${e.clientX}px`
      fireworks.style.top = `${e.clientY}px`
      document.body.appendChild(fireworks)

      setTimeout(() => {
        fireworks.remove()
      }, 1000)
    }
  }
}
</script>

<style>
.firework {
  position: absolute;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  box-shadow: 0 0 10px 5px rgba(255, 255, 0, 0.7);
  animation: explode 1s ease-out forwards;
}

@keyframes explode {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(20);
    opacity: 0;
  }
}
</style>

方法二:使用 Canvas 实现复杂效果

对于更复杂的烟花效果,可以使用 Canvas 进行绘制:

<template>
  <canvas ref="canvas" @click="launchFirework"></canvas>
</template>

<script>
export default {
  data() {
    return {
      particles: []
    }
  },
  mounted() {
    this.canvas = this.$refs.canvas
    this.ctx = this.canvas.getContext('2d')
    this.canvas.width = window.innerWidth
    this.canvas.height = window.innerHeight
    window.addEventListener('resize', this.resizeCanvas)
    this.animate()
  },
  methods: {
    resizeCanvas() {
      this.canvas.width = window.innerWidth
      this.canvas.height = window.innerHeight
    },
    launchFirework(e) {
      const particleCount = 100
      for (let i = 0; i < particleCount; i++) {
        this.particles.push({
          x: e.clientX,
          y: e.clientY,
          vx: Math.random() * 10 - 5,
          vy: Math.random() * 10 - 5,
          color: `hsl(${Math.random() * 360}, 100%, 50%)`,
          radius: Math.random() * 3 + 1,
          life: 100
        })
      }
    },
    animate() {
      requestAnimationFrame(this.animate)
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)

      this.particles.forEach((p, i) => {
        p.x += p.vx
        p.y += p.vy
        p.life--

        this.ctx.beginPath()
        this.ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2)
        this.ctx.fillStyle = p.color
        this.ctx.fill()

        if (p.life <= 0) {
          this.particles.splice(i, 1)
        }
      })
    }
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeCanvas)
  }
}
</script>

方法三:使用第三方库

可以集成专门的动画库如 particles.js 或 anime.js:

vue实现点击烟花

  1. 安装 anime.js:

    npm install animejs
  2. 在组件中使用:

    vue实现点击烟花

    
    <template>
    <div @click="playFirework" class="click-area"></div>
    </template>
import anime from 'animejs'

export default { methods: { playFirework(e) { const colors = ['#FF1461', '#18FF92', '#5A87FF', '#FBF38C'] const particles = 15

  for (let i = 0; i < particles; i++) {
    const particle = document.createElement('span')
    particle.style.position = 'absolute'
    particle.style.left = `${e.clientX}px`
    particle.style.top = `${e.clientY}px`
    particle.style.width = '10px'
    particle.style.height = '10px'
    particle.style.backgroundColor = colors[i % colors.length]
    particle.style.borderRadius = '50%'
    document.body.appendChild(particle)

    anime({
      targets: particle,
      translateX: () => anime.random(-100, 100),
      translateY: () => anime.random(-100, 100),
      scale: [1, 0],
      opacity: [1, 0],
      duration: 1000,
      easing: 'easeOutExpo',
      complete: () => particle.remove()
    })
  }
}

} }

```

注意事项

  • 性能优化:对于大量烟花粒子,建议使用 Canvas 而非 DOM 操作
  • 移动端适配:添加 touch 事件支持
  • 颜色随机化:使用 HSL 色彩空间可以生成更鲜艳的颜色
  • 清除机制:确保动画完成后移除 DOM 元素防止内存泄漏

以上方法可根据需求复杂度选择,CSS 方案最简单,Canvas 性能最好,第三方库功能最丰富。

标签: 烟花vue
分享给朋友:

相关文章

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录组…

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue toast实现

vue toast实现

Vue Toast 实现方法 使用第三方库(推荐) 安装 vue-toastification 库,这是一个功能丰富且轻量级的 Vue Toast 插件。 npm install vue-toast…

vue实现画板

vue实现画板

Vue 实现画板功能 在 Vue 中实现画板功能可以通过 HTML5 的 Canvas API 实现。以下是一个完整的实现方案: 创建 Vue 组件 新建一个 Vue 单文件组件(SFC),命名为…

react实现vue

react实现vue

React 实现 Vue 功能 React 和 Vue 是两种不同的前端框架,但可以通过一些方法在 React 中实现 Vue 的特性。以下是几种常见 Vue 功能在 React 中的实现方式: 双…