帮我写一段html烟花页面
时间: 2023-11-02 13:05:34 浏览: 25
<!DOCTYPE html>
<html>
<head>
<title>烟花特效</title>
<style>
#fireworks {
position: relative;
width: 100%;
height: 100%;
background-color: black;
}
#fireworks:before {
content: "";
display: block;
position: absolute;
top: 50%;
left: 50%;
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 10px white;
animation: sparkle 2s linear infinite;
}
@keyframes sparkle {
0% {
box-shadow: 0 0 10px white;
}
50% {
box-shadow: 0 0 20px white;
}
100% {
box-shadow: 0 0 10px white;
}
}
.firework {
position: absolute;
width: 20px;
height: 20px;
background-color: white;
border-radius: 50%;
animation: explode 1s ease-out;
}
@keyframes explode {
0% {
transform: scale(0);
opacity: 1;
}
50% {
transform: scale(1.5);
opacity: 0.5;
}
100% {
transform: scale(2);
opacity: 0;
}
}
.firework:before {
content: "";
display: block;
position: absolute;
top: 50%;
left: 50%;
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
transform: translate(-50%, -50%);
box-shadow: 0 0 10px white;
animation: sparkle 2s linear infinite;
}
.firework:after {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
width: 10px;
height: 10px;
background-color: white;
border-radius: 50%;
box-shadow: 0 0 10px white;
animation: sparkle 2s linear infinite;
}
</style>
</head>
<body>
<div id="fireworks">
<div class="firework" style="top: 50%; left: 50%;"></div>
</div>
<script>
// 获取屏幕宽度和高度
var screenWidth = window.innerWidth;
var screenHeight = window.innerHeight;
// 随机生成火花位置
function randomPos() {
var pos = {
x: Math.floor(Math.random() * screenWidth),
y: Math.floor(Math.random() * screenHeight)
};
return pos;
}
// 创建火花
function createFirework() {
var firework = document.createElement("div");
firework.classList.add("firework");
var pos = randomPos();
firework.style.top = pos.y + "px";
firework.style.left = pos.x + "px";
document.getElementById("fireworks").appendChild(firework);
setTimeout(function() {
firework.remove();
}, 1000);
}
// 循环创建火花
setInterval(function() {
createFirework();
}, 500);
</script>
</body>
</html>