ConfettiCelebration.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <view class="confetti-container" v-if="show" :style="{ '--font-scale': fontScale }">
  3. <view
  4. v-for="(particle, index) in particles"
  5. :key="index"
  6. class="confetti"
  7. :style="particle.style"
  8. >
  9. {{ particle.emoji }}
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import fontScaleMixin from '@/mixins/font-scale.js'
  15. export default {
  16. mixins: [fontScaleMixin],
  17. name: 'ConfettiCelebration',
  18. props: {
  19. show: { type: Boolean, default: false },
  20. duration: { type: Number, default: 2000 }
  21. },
  22. data() {
  23. return {
  24. particles: [],
  25. emojis: ['🎉', '⭐', '🌟', '✨', '💫', '🎊', '🏆', '🎁', '🌈', '💖']
  26. }
  27. },
  28. watch: {
  29. show(val) {
  30. if (val) {
  31. this.launch()
  32. }
  33. }
  34. },
  35. methods: {
  36. launch() {
  37. const particles = []
  38. for (let i = 0; i < 30; i++) {
  39. const x = Math.random() * 100
  40. const rotation = Math.random() * 360
  41. const scale = 0.5 + Math.random() * 0.5
  42. const duration = 1500 + Math.random() * 1000
  43. const delay = Math.random() * 300
  44. particles.push({
  45. x: x,
  46. y: -20,
  47. rotation: rotation,
  48. scale: scale,
  49. duration: duration,
  50. delay: delay,
  51. emoji: this.emojis[Math.floor(Math.random() * this.emojis.length)],
  52. style: `left: ${x}%; top: -20%; transform: rotate(${rotation}deg) scale(${scale}); animation: fall ${duration}ms ease-out ${delay}ms forwards;`
  53. })
  54. }
  55. this.particles = particles
  56. setTimeout(() => {
  57. this.particles = []
  58. }, this.duration)
  59. }
  60. }
  61. }
  62. </script>
  63. <style scoped>
  64. .confetti-container {
  65. position: fixed;
  66. top: 0;
  67. left: 0;
  68. width: 100%;
  69. height: 100%;
  70. pointer-events: none;
  71. z-index: 9999;
  72. overflow: hidden;
  73. }
  74. .confetti {
  75. position: absolute;
  76. font-size: 24px;
  77. animation-timing-function: ease-out;
  78. animation-fill-mode: forwards;
  79. }
  80. </style>
  81. <style>
  82. @keyframes fall {
  83. 0% {
  84. opacity: 1;
  85. transform: rotate(0deg) translateY(0) scale(1);
  86. }
  87. 100% {
  88. opacity: 0;
  89. transform: rotate(720deg) translateY(100vh) scale(0.5);
  90. }
  91. }
  92. </style>