Native Js method for achieving simple fireworks explosion effect, js fireworks explosion

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.