【HTML+CSS+JavaScript】实现鼠标点击烟花效果

【HTML+CSS+JavaScript】实现鼠标点击烟花效果(爆炸型、心型、圆形)

本文主要讲解三种烟花效果(爆炸型、心型、圆形),文章末尾附有完整的代码和图片获取链接。

一. 效果图

效果图(一) - 心型

请添加图片描述

效果图(二) - 圆型

请添加图片描述

效果图(三) - 爆炸型

请添加图片描述

二. 鼠标点击烟花效果 - 心型实现代码

(1) HTML部分代码

<!-- 引入外部的js代码 -->
<script type="text/javascript" src="buffermove1.js"></script> 

(2) CSS部分代码

* {
    padding: 0px;
    margin: 0px;
}

body {
    background: #000;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

(3) 内部的JavaScript部分代码

//this绑定的属性可以在整个构造函数内部都可以使用,而变量只能在函数内部使用。
function Fireworks(x, y) { //x,y鼠标的位置
    this.x = x;
    this.y = y;
    var that = this;
    //1.创建烟花。
    this.ceratefirework = function() {
        this.firework = document.createElement('div'); //整个构造函数内部都可以使用
        this.firework.style.cssText = `width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${document.documentElement.clientHeight}px;`;
        document.body.appendChild(this.firework);
        this.fireworkmove();
    };
    //2.烟花运动和消失
    this.fireworkmove = function() {
        buffermove(this.firework, {
            top: this.y
        }, function() {
            document.body.removeChild(that.firework); //烟花消失,碎片产生
            that.fireworkfragment();
        });
    };
    //3.创建烟花的碎片
    this.fireworkfragment = function() {
        var num = this.ranNum(30, 60); //盒子的个数
        this.perRadio = 2 * Math.PI / num; //弧度
        for (var i = 0; i < num; i++) {
            this.fragment = document.createElement('div');
            this.fragment.style.cssText = `width:5px;height:5px;background:rgb(${this.ranNum(0, 255)},${this.ranNum(0, 255)},${this.ranNum(0, 255)});position:absolute;left:${this.x}px;top:${this.y}px;`;
            document.body.appendChild(this.fragment);
            this.fireworkboom(this.fragment, i); //将当前创建的碎片传过去,方便运动和删除
        }
    }
    //4.碎片运动
    this.fireworkboom = function(obj, i) { //obj:创建的碎片
        var r = 0.1;
        obj.timer = setInterval(function() { //一个盒子运动
            r += 0.4;
            if (r >= 10) {
                clearInterval(obj.timer);
                document.body.removeChild(obj);
            }
            obj.style.left = that.x + 16 * Math.pow(Math.sin(that.perRadio * i), 3) * r + 'px';
            obj.style.top = that.y - (13 * Math.cos(that.perRadio * i) - 5 * Math.cos(2 * that.perRadio * i) - 2 * Math.cos(3 * that.perRadio * i) - Math.cos(4 * that.perRadio * i)) * r + 'px';
        }, 20);
    }
    //随机方法
    this.ranNum = function(min, max) {
        return Math.round(Math.random() * (max - min)) + min;
    };
}
document.onclick = function(ev) {
    var ev = ev || window.event;
    new Fireworks(ev.clientX, ev.clientY).ceratefirework();
}

三. 鼠标点击烟花效果 - 圆型实现代码

(1) HTML部分代码

<!-- 引入外部的js代码 -->
<script type="text/javascript" src="buffermove1.js"></script> 

(2) CSS部分代码

* {
    padding: 0px;
    margin: 0px;
}

body {
    background: #000;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

(3) 内部的JavaScript部分代码

//this绑定的属性可以在整个构造函数内部都可以使用,而变量只能在函数内部使用。
function Fireworks(x, y) { //x,y鼠标的位置
    this.x = x;
    this.y = y;
    var that = this;
    //1.创建烟花。
    this.ceratefirework = function() {
        this.firework = document.createElement('div'); //整个构造函数内部都可以使用
        this.firework.style.cssText = `width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${document.documentElement.clientHeight}px;`;
        document.body.appendChild(this.firework);
        this.fireworkmove();
    };
    //2.烟花运动和消失
    this.fireworkmove = function() {
        var that = this;
        buffermove(this.firework, {
            top: this.y
        }, function() {
            document.body.removeChild(that.firework); //烟花消失,碎片产生
            that.fireworkfragment();
        });
    };
    //3.创建烟花的碎片
    this.fireworkfragment = function() {
        var num = this.ranNum(30, 60); //盒子的个数
        this.perRadio = 2 * Math.PI / num; //弧度
        for (var i = 0; i < num; i++) {
            this.fragment = document.createElement('div');
            this.fragment.style.cssText = `width:5px;height:5px;background:rgb(${this.ranNum(0, 255)},${this.ranNum(0, 255)},${this.ranNum(0, 255)});position:absolute;left:${this.x}px;top:${this.y}px;`;
            document.body.appendChild(this.fragment);
            this.fireworkboom(this.fragment, i); //将当前创建的碎片传过去,方便运动和删除
        }
    }
    //4.碎片运动
    this.fireworkboom = function(obj, i) { //obj:创建的碎片
        var r = 10;
        obj.timer = setInterval(function() { //一个盒子运动
            r += 4;
            if (r >= 200) {
                clearInterval(obj.timer);
                document.body.removeChild(obj);
            }
            obj.style.left = that.x + Math.sin(that.perRadio * i) * r + 'px';
            obj.style.top = that.y + Math.cos(that.perRadio * i) * r + 'px';
        }, 20);
    }
    //随机方法
    this.ranNum = function(min, max) {
        return Math.round(Math.random() * (max - min)) + min;
    };
}
document.onclick = function(ev) {
    var ev = ev || window.event;
    new Fireworks(ev.clientX, ev.clientY).ceratefirework();
}

四. 鼠标点击烟花效果 - 爆炸型实现代码

(1) HTML部分代码

<!-- 引入外部的js代码 -->
<script type="text/javascript" src="buffermove1.js"></script> 

(2) CSS部分代码

* {
    padding: 0px;
    margin: 0px;
}

body {
    background: #000;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

(3) 内部的JavaScript部分代码

//this绑定的属性可以在整个构造函数内部都可以使用,而变量只能在函数内部使用。
function Fireworks(x, y) { //x,y鼠标的位置
    this.x = x;
    this.y = y;
    var that = this;
    //1.创建烟花。
    this.ceratefirework = function() {
        this.firework = document.createElement('div'); //整个构造函数内部都可以使用
        this.firework.style.cssText = `width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${document.documentElement.clientHeight}px;`;
        document.body.appendChild(this.firework);
        this.fireworkmove();
    };
    //2.烟花运动和消失
    this.fireworkmove = function() {
        buffermove(this.firework, {
            top: this.y
        }, function() {
            document.body.removeChild(that.firework); //烟花消失,碎片产生
            that.fireworkfragment();
        });
    };
    //3.创建烟花的碎片
    this.fireworkfragment = function() {
        for (var i = 0; i < this.ranNum(30, 60); i++) {
            this.fragment = document.createElement('div');
            this.fragment.style.cssText = `width:5px;height:5px;background:rgb(${this.ranNum(0, 255)},${this.ranNum(0, 255)},${this.ranNum(0, 255)});position:absolute;left:${this.x}px;top:${this.y}px;`;
            document.body.appendChild(this.fragment);
            this.fireworkboom(this.fragment); //将当前创建的碎片传过去,方便运动和删除
        }
    }

    //4.碎片运动
    this.fireworkboom = function(obj) { //obj:创建的碎片

        //设点速度(值不同,正负符号不同)
        var speedx = parseInt((Math.random() > 0.5 ? '-' : '') + this.ranNum(1, 15));
        var speedy = parseInt((Math.random() > 0.5 ? '-' : '') + this.ranNum(1, 15));

        //初始速度
        var initx = this.x;
        var inity = this.y;
        obj.timer = setInterval(function() { //一个盒子运动
            initx += speedx;
            inity += speedy;
            if (inity >= document.documentElement.clientHeight) {
                clearInterval(obj.timer);
                document.body.removeChild(obj);
            }
            obj.style.left = initx + 'px';
            obj.style.top = inity + 'px';
        }, 20);

    }

    //随机方法
    this.ranNum = function(min, max) {
        return Math.round(Math.random() * (max - min)) + min;
    };
}


document.onclick = function(ev) {
    var ev = ev || window.event;
    new Fireworks(ev.clientX, ev.clientY).ceratefirework();
}

五. 共同引用的JavaScript部分代码

function getstyle(obj, attr) {
    if (window.getComputedStyle) {
        //标准
        return getComputedStyle(obj)[attr]
    } else {
        //IE
        return obj.currentStyle[attr]
    }
}

function buffermove(obj, json, fn) {
    var speed = 0
    clearInterval(obj.timer)
    obj.timer = setInterval(function() {
        var bstop = true
        for (var attr in json) {
            var currentvalue = 0
            if (attr === 'opacity') {
                currentvalue = Math.round(getstyle(obj, attr) * 100)
            } else {
                currentvalue = parseInt(getstyle(obj, attr))
            }
            speed = (json[attr] - currentvalue) / 10
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)
            if (currentvalue != json[attr]) {
                if (attr === 'opacity') {
                    obj.style.opacity = (currentvalue + speed) / 100
                    obj.style.filter = 'alpha(opacity:' + (currentvalue + speed) + ')'
                } else {
                    obj.style[attr] = currentvalue + speed + 'px'
                }
                bstop = false
            }
        }
        if (bstop) {
            clearInterval(obj.timer)
            fn && fn()
        }
    }, 5)
}

六. 完整的代码和图片获取

:细心地网友估计已经发现三种效果的CSS和HTML这两部分的代码其实是一样的。

完整的代码和图片获取方式

链接:https://pan.baidu.com/s/16kbVr5pOcBqxdCRfs-GJnA?pwd=yhxg 
提取码:yhxg
  • 12
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用HTMLCSSJavaScript来生成一个放烟花的页面。首先,使用HTML编写网页结构;其次,使用CSS设计网页样式;最后,使用JavaScript编写烟花动画效果。有很多开源的JavaScript库,如Three.js、p5.js等,可以帮助您快速生成烟花效果。 ### 回答2: 要用HTMLCSSJavaScript来生成一个放烟花的页面,可以按照以下步骤进行: 1. 创建一个HTML文件,使用`<canvas>`元素来绘制烟花效果的画布。在`<body>`标签中添加一个`<canvas>`元素,并设置宽度和高度,以便适应屏幕。 2. 使用CSS样式来美化页面。可以设置背景颜色、字体样式、页面布局等。 3. 在JavaScript中编写烟花效果的代码。可以使用Canvas API来绘制烟花的形状、颜色和动画效果。可以创建烟花的粒子,并进行位置、速度和加速度的更新。通过循环不断更新烟花的状态,实现绚丽的动画效果。 4. 可选择添加交互效果。可以在页面加载完成后监听鼠标点击事件,在指定位置触发烟花效果。通过JavaScript来捕获鼠标点击位置,并在该位置生成烟花效果。 5. 最后,在HTML文件中引入CSS样式和JavaScript脚本,确保页面正常加载。 通过以上步骤,就可以使用HTMLCSSJavaScript来生成一个放烟花的页面。可以通过调整代码中的参数来调整烟花效果的颜色、形状和动画速度,以满足个性化的需求。 ### 回答3: 要使用HTMLCSSJavaScript生成一个放烟花的页面,可以按照以下步骤进行: 1. 创建一个HTML文件,并添加一个具有id的div元素,用于放置烟花效果。 ```html <!DOCTYPE html> <html> <head> <title>放烟花页面</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div id="fireworks"></div> <script src="script.js"></script> </body> </html> ``` 2. 创建一个CSS文件,并为烟花效果的div元素添加样式,使其具有适当的位置和尺寸。 ```css #fireworks { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 500px; height: 500px; background-color: black; } ``` 3. 创建一个JavaScript文件,并在其中编写生成烟花效果的代码。 ```javascript window.onload = function() { var fireworks = document.getElementById("fireworks"); var fireworksColors = ["red", "green", "blue", "yellow", "orange"]; function createFirework() { var firework = document.createElement("div"); firework.classList.add("firework"); firework.style.backgroundColor = fireworksColors[Math.floor(Math.random() * fireworksColors.length)]; firework.style.top = Math.random() * 100 + "%"; firework.style.left = Math.random() * 100 + "%"; fireworks.appendChild(firework); setTimeout(function() { firework.style.transform = "translate(0, -1000%)"; firework.style.opacity = "0"; }, 10); setTimeout(function() { firework.remove(); }, 1500); } setInterval(function() { createFirework(); }, 1000); }; ``` 4. 在HTML文件中的```<head>```部分引入所创建的CSSJavaScript文件。 5. 在浏览器中打开该HTML文件,即可看到生成烟花效果的页面。 这是一个简单的例子,你可以根据自己的需求进一步扩展和美化烟花效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值