firework—烟花源码(01)

简介: firework—烟花源码(01)
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>烟花绽放效果</title>
  <style>
    *{margin:0;padding: 0;}
    html{height: 100%;}
    body{height: 100%;background: #000;overflow: hidden;}
    .box{width: 5px;height: 30px;border-radius: 50%;background: red;position: absolute;}
    .yanhua{width: 8px;height: 8px;border-radius: 100%;background: rgb(253, 253, 253);position: absolute;}
  </style>
</head>
<body>
  <script type="text/javascript">
    document.addEventListener('click',function(e){
      //获取鼠标点击的位置
      var e = e || window.event;
      var x = e.clientX;
      var y = e.clientY;
      var h = document.body.clientHeight;
      //创建一个div  
      var oDiv = document.createElement("div"); 
      //属性设置
      oDiv.className = 'box';
      oDiv.style.left = x + 'px';
      oDiv.style.top = h + 'px';
      oDiv.style.background = color1();
       //将元素添加到body  
       document.body.appendChild(oDiv);  
       //将div移动到点击位置
    var time =  setInterval(function(){
        oDiv.style.top = oDiv.offsetTop - 10  + 'px';
        if(oDiv.offsetTop <= y){
          clearInterval(time);
          oDiv.style.top = y + 'px';
          document.body.removeChild(oDiv);  
          //炸开的烟花
          yanhua(x,y,h);
        }
      },1000/60);
    })
      //小烟花函数
      function yanhua(x,y,h){
        var ind = Math.round( Math.random()*50)+10;
        var fires = [];
        for(var i = 0 ; i < ind; i++){
          fires[i] = document.createElement("div"); 
          fires[i].className = 'yanhua';
          fires[i].style.left = x + 'px';
          fires[i].style.top = y + 'px';
          fires[i].style.background = color1();
          fires[i].weizhiX = Math.round( Math.random()*20 - 10);
          fires[i].weizhiY = Math.round( Math.random()*20 - 10);
          document.body.appendChild(fires[i]);
        } 
        //炸开
        setInterval(function(){
          for(var j = 0;j<ind;j++){
            fires[j].style.left = fires[j].offsetLeft + fires[j].weizhiX + 'px';
            fires[j].style.top = fires[j].offsetTop + fires[j].weizhiY + 'px';
            fires[j].weizhiY +=0.5;
            if(fires[j].offsetTop > h || fires[j].offsetLeft < 0 || fires[j].offsetLeft > document.body.clientWidth){
              document.body.removeChild(fires[j]); 
              // clearInterval(timet);
            }
          }
          },1000/60);  
      }
      //随机颜色
      function color1(){
        var r = Math.round( Math.random()*255);
        var g = Math.round( Math.random()*255);
        var b = Math.round( Math.random()*255);
        return 'rgb('+ r +','+ g +','+ b +')';
      }
      function color2(){
        return '#' + (~~(Math.random()*(1<<24))).toString(16);
      }
  </script>
</body>
</html>

a25a476f9774f15db32c4637be9d12ed.gif

目录
打赏
0
0
0
0
11
分享
相关文章
HTML+CSS如何打造撒花动画效果?3分钟学会,炫酷到爆!
HTML+CSS如何打造撒花动画效果?3分钟学会,炫酷到爆!
画烟花
进行相关代码编写,完成画烟花这个项目。
393 2
除夕最炫烟花代码
除夕最炫烟花代码
270 0
【兔年烟花】旖旎风景——浪漫烟花(Python实现)
【兔年烟花】旖旎风景——浪漫烟花(Python实现)
166 0
2023年新年烟花代码(背景音乐完整版)
2023年新年烟花代码(背景音乐完整版)
313 0
2023跨年烟花(浪漫烟花+美妙音乐+雪花飘飘)含前端源码直接下载---系列最终篇
2023跨年烟花(浪漫烟花+美妙音乐+雪花飘飘)含前端源码直接下载---系列最终篇
673 0
2023跨年烟花(浪漫烟花+美妙音乐+雪花飘飘)含前端源码直接下载---系列最终篇
uiu
烟花代码,予心上人最璀璨烟花—— 附源码与成品(HTML+CSS+JS)
烟花代码,予心上人最璀璨烟花—— 附源码与成品(HTML+CSS+JS)
uiu
3365 0
烟花代码,予心上人最璀璨烟花—— 附源码与成品(HTML+CSS+JS)
2023将至,前端程序员们应该一起放个烟花庆祝一下,走起
前言:小时候,在我印象中,每到快过年的时候就有很多卖炮仗的,一般也就是阳历的12月份到明年的正月15号卖炮仗的商家比较多,省下买辣条的钱去买炮仗,在老家也就过年和除夕两天及正月15日这几天放烟花和炮仗比较猛,现在年纪大了,听不得炮仗那种噪声了,也考虑到环保,工作之后的程序员以代码的形式演绎一下烟花的效果。
323 0
2023将至,前端程序员们应该一起放个烟花庆祝一下,走起