<!DOCTYPE html>
<html>
<head>
<title>包含烟花背景的页面</title>
<style type="text/css">
/* 烟花样式 */
.fire {
display: block;
overflow: hidden;
font-size: 12px;
position: absolute;
}
/* 确保烟花在背景,不影响原页面内容 */
body {
position: relative;
overflow: hidden;
background: #000;
}
html {
overflow: hidden;
background: #000;
}
/* 用于放置烟花的容器,确保在最底层 */
.fireworks-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
</style>
</head>
<body>
<!-- 这里可以放置你原有的 HTML 内容 -->
<h1>这是页面的主要内容</h1>
<p>这里可以添加更多的文本或其他元素。</p>
<!-- 烟花容器 -->
<div class="fireworks-container"></div>
<script type="text/javascript">
// 烟花粒子类
var Fire = function (r, color) {
this.radius = r || 12;
this.color = color;
this.xpos = 0;
this.ypos = 0;
this.zpos = 0;
this.vx = 0;
this.vy = 0;
this.vz = 0;
this.mass = 1;
this.x = 0;
this.y = 0;
this.p = document.createElement("span");
this.p.className = "fire";
this.p.innerHTML = "*";
this.p.style.fontSize = this.radius + "px";
this.p.style.color = "#" + this.color;
}
// 烟花粒子原型方法
Fire.prototype = {
append: function (parent) {
parent.appendChild(this.p);
},
setSize: function (scale) {
this.p.style.fontSize = this.radius * scale + "px";
},
setPosition: function (x, y) {
this.p.style.left = x + "px";
this.p.style.top = y + "px";
},
setVisible: function (b) {
this.p.style.display = b ? "block" : "none";
}
}
// 烟花效果类
var fireworks = function () {
var fires = new Array();
var count = 150;
var fl = 250;
var vpx = 500;
var vpy = 300;
var gravity = .5;
var floor = 200;
var bounce = -.8;
var timer;
var wind = ((Math.floor(Math.random() * 3) + 3) / 10) * (Math.random() * 2 - 1 > 0 ? 1 : -1) * .25;
var wpos = 0;
var wcount;
var container = document.querySelector('.fireworks-container');
return {
init: function () {
wcount = 50 + Math.floor(Math.random() * 100);
for (var i = 0; i < count; i++) {
var color = 0xFF0000;
color = (Math.random() * 0xFFFFFF).toString(16).toString().split(".")[0];
while (color.length < 6) {
color = "0" + color;
}
var fire = new Fire(12, color);
fires.push(fire);
fire.ypos = -100;
fire.vz = Math.random() * 6 - 3;
fire.vx = (Math.random() * 2 - 1) * 2;
fire.vy = Math.random() * -15 - 15;
fire.x = 500;
fire.y = 600;
fire.append(container);
}
var that = this;
timer = setInterval(function () {
wpos++;
if (wpos >= wcount) {
wpos = 0;
wcount = 50 + Math.floor(Math.random() * 100);
wind = ((Math.floor(Math.random() * 3) + 3) / 10) * (Math.random() * 2 - 1 > 0 ? 1 : -1) * .25;
}
for (var i = 0; i < count; i++) {
that.move(fires[i]);
}
}, 30);
},
move: function (fire) {
fire.vy += gravity;
fire.x += fire.vx;
fire.y += fire.vy;
fire.vx += wind;
fire.setPosition(fire.x, fire.y);
if (fire.x < 0 || fire.x > window.innerWidth || fire.y < 0 || fire.y > window.innerHeight) {
fire.vx = (Math.random() * 2 - 1) * 2;
fire.vy = Math.random() * -15 - 15;
fire.x = window.innerWidth / 2;
fire.y = window.innerHeight;
fire.setPosition(fire.x, fire.y);
}
}
}
}
// 初始化烟花效果
fireworks().init();
</script>
</body>
</html>
index.html
index.html