Native Js method for achieving simple fireworks explosion effect, js fireworks explosion
This article describes how to implement simple fireworks explosion with native Js. Share it with you for your reference. The specific analysis is as follows:
Implementation principle: randomly generate some divs within a certain range to form the fireworks Effect
Copy codeThe Code is as follows: <! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> js fireworks </title>
<Script type = "text/javascript">
Document. onclick = function (ev)
{
Var oEvent = ev | event;
Var aDiv = [];
Var oDiv = null;
Var _ oDiv = document. createElement ('div ');
Var I = 0;
Var x = oEvent. clientX;
Var y = oEvent. clientY;
_ ODiv. style. position = 'absolute ';
_ ODiv. style. background = 'red ';
_ ODiv. style. width = '3px ';
_ ODiv. style. height = '30px ';
_ ODiv. style. left = oEvent. clientX + 'px ';
_ODiv.style.top=document.doc umentElement. clientHeight + 'px ';
Document. body. appendChild (_ oDiv );
Var t = setInterval (function (){
If (_ oDiv. offsetTop <= y)
{
ClearInterval (t );
Show ();
Document. body. removeChild (_ oDiv );
}
_ ODiv. style. top = _ oDiv. offsetTop-30 + 'px ';
}, 30 );
Function show ()
{
Var oDiv = null;
For (I = 0; I <100; I ++)
{
ODiv = document. createElement ('div ');
ODiv. style. width = '3px ';
ODiv. style. height = '3px ';
ODiv. style. background = '#' + Math. ceil (Math. random () * 0 xEFFFFF + 0x0FFFFF). toString (16 );
ODiv. style. position = 'absolute ';
ODiv. style. left = x + 'px ';
ODiv. style. top = y + 'px ';
Var a = Math. random () * 360;
// ODiv. speedX = Math. random () * 40-20;
// ODiv. speedY = Math. random () * 40-20;
ODiv. speedX = Math. sin (a x 180/Math. PI) * 20 * Math. random ();
ODiv. speedY = Math. cos (a x 180/Math. PI) * 20 * Math. random ();
Document. body. appendChild (oDiv );
ADiv. push (oDiv );
}
}
SetInterval (doMove, 30 );
Function doMove ()
{
For (I = 0; I <aDiv. length; I ++)
{
ADiv [I]. style. left = aDiv [I]. offsetLeft + aDiv [I]. speedX + 'px ';
ADiv [I]. style. top = aDiv [I]. offsetTop + aDiv [I]. speedY + 'px ';
ADiv [I]. speedY + = 1;
If (aDiv [I]. offsetLeft <0 | aDiv [I]. offsetLeft> document.doc umentElement. clientWidth | aDiv [I]. offsetTop <0 | aDiv [I]. offsetTop> document.doc umentElement. clientHeight)
{
Document. body. removeChild (aDiv [I]);
ADiv. splice (I, 1 );
}
}
}
};
</Script>
</Head>
<Body style = "overflow: hidden; background: black;">
</Body>
</Html>
I hope this article will help you design javascript programs.