【HTML+CSS+JavaScript】实现鼠标点击烟花效果
创始人
2024-05-02 06:54:06
0

文章目录

  • 【HTML+CSS+JavaScript】实现鼠标点击烟花效果(爆炸型、心型、圆形)
    • 一. 效果图
    • 二. 鼠标点击烟花效果 - 心型实现代码
      • (1) HTML部分代码
      • (2) CSS部分代码
      • (3) 内部的JavaScript部分代码
    • 三. 鼠标点击烟花效果 - 圆型实现代码
      • (1) HTML部分代码
      • (2) CSS部分代码
      • (3) 内部的JavaScript部分代码
    • 四. 鼠标点击烟花效果 - 爆炸型实现代码
      • (1) HTML部分代码
      • (2) CSS部分代码
      • (3) 内部的JavaScript部分代码
    • 五. 共同引用的JavaScript部分代码
    • 六. 完整的代码和图片获取

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

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

一. 效果图

效果图(一) - 心型

请添加图片描述

效果图(二) - 圆型

请添加图片描述

效果图(三) - 爆炸型

请添加图片描述

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

(1) HTML部分代码


 

(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部分代码


 

(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部分代码


 

(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 {//IEreturn obj.currentStyle[attr]}
}function buffermove(obj, json, fn) {var speed = 0clearInterval(obj.timer)obj.timer = setInterval(function() {var bstop = truefor (var attr in json) {var currentvalue = 0if (attr === 'opacity') {currentvalue = Math.round(getstyle(obj, attr) * 100)} else {currentvalue = parseInt(getstyle(obj, attr))}speed = (json[attr] - currentvalue) / 10speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed)if (currentvalue != json[attr]) {if (attr === 'opacity') {obj.style.opacity = (currentvalue + speed) / 100obj.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

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...