原生Js实现简易烟花爆炸效果的方法
网络编程 2021-07-04 21:47www.168986.cn编程入门
这篇文章主要介绍了原生Js实现简易烟花爆炸效果的方法,实例分析了javascript实现烟花爆炸效果的原理与具体实现方法,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了原生Js实现简易烟花爆炸效果的方法。分享给大家供大家参考。具体分析如下
实现原理 在一定范围内,随机生成一些div,形成烟花效果
代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js烟花效果</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.=document.documentElement.clientHeight+'px';
document.body.appendChild(_oDiv);
var t=setInterval(function (){
if(_oDiv.offsetTop<=y)
{
clearInterval(t);
show();
document.body.removeChild(_oDiv);
}
_oDiv.style.=_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()0xEFFFFF+0x0FFFFF).toString(16);
oDiv.style.position='absolute';
oDiv.style.left=x+'px';
oDiv.style.=y+'px';
var a=Math.random()360;
//oDiv.speedX=Math.random()40-20;
//oDiv.speedY=Math.random()40-20;
oDiv.speedX=Math.sin(a180/Math.PI)20Math.random();
oDiv.speedY=Math.cos(a180/Math.PI)20Math.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.=aDiv[i].offsetTop+aDiv[i].speedY+'px';
aDiv[i].speedY+=1;
if(aDiv[i].offsetLeft<0 || aDiv[i].offsetLeft>document.documentElement.clientWidth || aDiv[i].offsetTop<0 || aDiv[i].offsetTop>document.documentElement.clientHeight)
{
document.body.removeChild(aDiv[i]);
aDiv.splice(i, 1);
}
}
}
};
</script>
</head>
<body style="overflow:hidden; background:black;">
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>js烟花效果</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.=document.documentElement.clientHeight+'px';
document.body.appendChild(_oDiv);
var t=setInterval(function (){
if(_oDiv.offsetTop<=y)
{
clearInterval(t);
show();
document.body.removeChild(_oDiv);
}
_oDiv.style.=_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()0xEFFFFF+0x0FFFFF).toString(16);
oDiv.style.position='absolute';
oDiv.style.left=x+'px';
oDiv.style.=y+'px';
var a=Math.random()360;
//oDiv.speedX=Math.random()40-20;
//oDiv.speedY=Math.random()40-20;
oDiv.speedX=Math.sin(a180/Math.PI)20Math.random();
oDiv.speedY=Math.cos(a180/Math.PI)20Math.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.=aDiv[i].offsetTop+aDiv[i].speedY+'px';
aDiv[i].speedY+=1;
if(aDiv[i].offsetLeft<0 || aDiv[i].offsetLeft>document.documentElement.clientWidth || aDiv[i].offsetTop<0 || aDiv[i].offsetTop>document.documentElement.clientHeight)
{
document.body.removeChild(aDiv[i]);
aDiv.splice(i, 1);
}
}
}
};
</script>
</head>
<body style="overflow:hidden; background:black;">
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指