1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>点击产生类似烟花效果</title> </head> <body> </body> <script> const Colors = [ "red", "blue", "green", "gary", "orange", "pink", "yellow", "purple", "black", ] ; const particlesNumber = 20; function creatParticle(x,y){ const ele = document.createElement("div"); ele.style.height = "10px"; ele.style.width = "10px"; ele.style.borderRadius = "5px"; ele.style.position = "absolute"; ele.style.top = `${y}px`; ele.style.left = `${x}px`; ele.style.transform = "transform(-50%,-50%)"; ele.style.backgroundColor = Colors[Math.floor(Math.random()*Colors.length)];
const animation = ele.animate( [ { transform:`translate(${(Math.random()-0.5)*500}px,${(Math.random()-0.5)*500}px) rotate(${Math.random()*520}deg)`, opacity: 0 }, ], { duration: 1000, iterations: 1 } ); document.body.appendChild(ele); animation.onfinish = () => ele.remove(); } document.addEventListener("click",(e) =>{ const {clientX: x, clientY: y } = e;
for (let index = 0; index < particlesNumber; index++) { creatParticle(x,y); } }); </script> </html>
|