用代码写个烟花之基础版

今天用代码来写个烟花吧,先写个基础版的,后面的版本后续继续更新
下面上代码:

1、这是html+css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #container{
            width: 80%;
            height: 600px;
            border: 2px solid red;
            background: #000;
            margin:20px auto;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .fire{
            width: 10px;
            height:10px;
            position: absolute;
            bottom: 0;
        }
        .small-fire{
            width: 10px;
            height:10px;
            position: absolute;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div id="container"></div>

2、下面是js代码(面向对象写的)

<!-- 这是我自己封装的一个运动函数,大家可以直接引入调用 -->
<script src="../move.js"></script>
<script>

    // 分析:
        // 创建主体元素
        // 设置样式,初始位置,拿到鼠标点击时传的坐标
        // 向上运动,目标:鼠标的坐标
        // 结束,删除主体元素
        // 创建一批小元素,设置初始位置为鼠标点击的坐标
        // 运动到随机目标,结束,删除所有


    // 设计:
        // class Fire{
        //     constructor(pos){
        //         // 接收参数,处理参数,定义属性
        //     }
        //     createFire(){
        //         // 创建主体元素
        //         // 设置位置
        //         // 设置样式
        //         // 开始运动
        //         this.fireMove();
        //     }
        //     fireMove(){
        //         // 开始运动
        //         move(主体烟花,属性和目标,()=>{
        //             // 删除主体烟花
        //             // 创建小烟花
        //             this.createSmall()
        //         })
        //     }
        //     createSmall(){
        //         // 创建小烟花
        //         // 设置初始位置
        //         // 设置样式
        //         // 开始运动
        //         this.smallMove();
        //     }
        //     smallMove(){
        //         move(小烟花,属性和目标,()=>{
        //             // 删除小烟花
        //         })
        //     }
        // }
        // var ocont = document.getElementById("container");
        // ocont.onclick = function(){
        //     new Fire(坐标);
        // }
    // 编程
        class Fire{
            constructor(ops){
                // 接收参数,处理参数,定义属性
                this.cont = ops.cont;
                this.x = ops.x;
                this.y = ops.y;

                this.createFire();
            }
            createFire(){
                // 创建主体元素
                this.ele = document.createElement("div");
                // 设置样式
                this.ele.className = "fire";
                this.ele.style.background = randomColor();
                // 设置位置
                this.ele.style.left = this.x + "px";
                this.cont.appendChild(this.ele);
                // 开始运动
                this.fireMove();
            }
            fireMove(){
                // 开始运动
                move(this.ele,{top:this.y},()=>{
                    // 删除主体烟花
                    this.ele.remove();
                    // 创建小烟花
                    this.createSmall()
                })
            }
            createSmall(){
                var num = random(10,20);
                // 创建小烟花
                for(var i=0;i<num;i++){
                    var small = document.createElement("div");
                    // 设置样式
                    small.className = "small-fire";
                    small.style.background = randomColor();
                    small.setAttribute("index",i);
                    // 设置初始位置
                    small.style.left = this.x + "px";
                    small.style.top = this.y + "px";
                    this.cont.appendChild(small);
                    // console.log(small);
                    // 开始运动
                    this.smallMove(small);
                }
            }
            smallMove(ele){
                // 随机目标
                var x = random(0,this.cont.offsetWidth-ele.offsetWidth);
                var y = random(0,this.cont.offsetHeight-ele.offsetHeight);
                // 开始运动
                move(ele,{left:x,top:y},()=>{
                    // 删除小烟花
                    ele.remove();
                })
            }
        }

        var ocont = document.getElementById("container");
        ocont.onclick = function(eve){
            var e = eve || window.event;
            // 每次点击时,创建烟花对象,并传入当前点击的坐标和容器标签
            new Fire({
                cont:ocont,
                y:e.clientY-ocont.offsetTop,
                x:e.clientX-ocont.offsetLeft
            });
        }


    
        // 这是封装的随机数
    function random(a,b){
        return Math.round(Math.random()*(a-b)+b);
    }
    // 这是封装的随机颜色
    function randomColor(){
        return `rgb(${random(0,255)},${random(0,255)},${random(0,255)})`;
    }

</script>
</body>
</html>

3、下面是封装的move函数,可直接引入调用

function move(ele, data, cb){
    clearInterval(ele.t);
    ele.t = setInterval(() => {
        var onoff = true;
        for(var attr in data){
            var iNow = attr === "opacity" ? getStyle(ele, attr) * 100 : parseInt(getStyle(ele, attr));

            var speed = (data[attr] - iNow)/10;
            speed = speed<0 ? Math.floor(speed) : Math.ceil(speed);

            if(data[attr] !== iNow) onoff = false;

            ele.style[attr] = attr === "opacity" ? (iNow + speed)/100 : iNow + speed + "px";
        }
        if(onoff){
            clearInterval(ele.t);
            cb && cb();
        }
    }, 30);
}

这是效果
在这里插入图片描述

好了,大家把代码复制到自己的编辑器可以试试,终于可以自己放烟花了,记得调用move.js,不然可用不了在这里插入图片描述

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值