zoukankan      html  css  js  c++  java
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例

    原生js实现放烟花效果,点击鼠标,然后随机向四周扩散,!

    思路:

    1.首先烟花是五颜六色的,所以我们先写一个随机颜色的函数;

    2.创建一个制造烟花的构造函数,第一个参数为元素,第二参数为初始x轴位置,第三参数为y轴位置;

    3.烟花散开的位置是随机的,所以我们先要使用随机数生成一个随机的速度值。

    代码内有详细注释,生成的烟花形状可以根据自身需要调成图片或者其它样式。

    效果图如下:

    代码如下:

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <STYLE>
            *{
                padding: 0px;
                margin: 0px;
                background: #000;
            }
            /*预设置烟花的大小*/
            .firworks{
                 4px;
                height: 4px;
                position: absolute;
            }
        </STYLE>
    </head>
    <body>
    
    </body>
    
    <script type="text/javascript">
        //封装一个颜色随机的效果
        function randomColor(){
            var color = "rgb("
            var r = parseInt(Math.random()*256);
            var g = parseInt(Math.random()*256);
            var b = parseInt(Math.random()*256);
            color = color+r+","+g+","+b+")";
            return color;
        }
        //创建一个制造烟花的构造函数,第一个参数为元素,第二参数为初始x轴位置,第三参数为y轴位置。
        function Fireworks(Div,x,y){
            Div.style.backgroundColor=randomColor();   //给烟花添加背景色
            Div.className="firworks";                    //添加一个class
            document.body.appendChild(Div);
            Div.style.left=x+"px";                        //把鼠标点击坐标给div
            Div.style.top=y+"px";
            var speedX = (parseInt(Math.random()*2) == 0 ? 1 : -1)*parseInt(Math.random()*16 + 1);  //三目运算符随机移动方向,概率50%,为1时往正方向移动,负1时往反方向移动第二个随机数随机速度快慢
            var speedY = (parseInt(Math.random()*2) == 0 ? 1 : -1)*parseInt(Math.random()*20 + 1);
            this.move=function(){
                var i = 3;
                var time1=setInterval(function(){
                    i++;
                    Div.style.left=Div.offsetLeft+speedX+"px";
                    Div.style.top=Div.offsetTop+speedY+i+"px";   //当i+speedY>0时,烟花朝下运动。
                    if(Div.offsetLeft+Div.offsetWidth>window.innerWidth|| Div.offsetLeft<2 || Div.offsetTop+Div.offsetHeight>window.innerHeight || Div.offsetTop<2 ){
                        Div.remove();        //移动出可视区域记得删除div和清除定时器
                        clearInterval(time1);
                    }
                },30);
            }
        }
        document.onclick=function (e){
            var evt=e||window.event;    //兼容性处理
            for(var i=0;i<80;i++){        //随机烟花的数量
                var div=document.createElement("div");
                var b=new Fireworks(div,evt.pageX,evt.pageY);
                b.move();
            }
        }
    </script>
    </html>
  • 相关阅读:
    镜像源收集
    关于vue-cli3脚手架安装后回退到vue-cli2版本的问题
    window.location 对象
    常用正则表达式
    前端开发工程师面试题
    面试题1
    Echarts.js使用
    swipe.js 使用方法
    canvas基础API
    前端面试题集锦
  • 原文地址:https://www.cnblogs.com/zhaohongcheng/p/10929314.html
Copyright © 2011-2022 走看看