• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

原生Js 简易烟花爆炸效果

JavaScript 水墨上仙 1589次浏览

实现原理: 在一定范围内,随机生成一些div,形成烟花效果

<!--
智能社© - http://www.zhinengshe.com/
 
微博:@北京智能社
微信:zhi_neng_she
 
最具深度的前端开发培训机构 HTML+CSS/JS/HTML5
-->
 
 
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>烟花 - 智能社 - www.zhinengshe.com</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.documentElement.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()*0xEFFFFF+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*180/Math.PI)*20*Math.random();
            oDiv.speedY=Math.cos(a*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.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>

附件:源代码下载


喜欢 (0)
加载中……