2024跨年倒计时烟花代码html
时间: 2024-01-01 13:05:03 浏览: 118
2024跨年烟花代码html 附示例代码
5星 · 资源好评率100%
很抱歉,我无法提供HTML代码。但是,你可以使用JavaScript来实现一个简单的倒计时烟花效果。以下是一个示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>2024跨年倒计时烟花</title>
<style>
#fireworks {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 9999;
pointer-events: none;
}
</style>
</head>
<body>
<h1>2024跨年倒计时烟花</h1>
<div id="countdown"></div>
<canvas id="fireworks"></canvas>
<script>
// 设置倒计时时间
var countdownDate = new Date("Jan 1, 2024 00:00:00").getTime();
// 更新倒计时
var countdown = setInterval(function() {
var now = new Date().getTime();
var distance = countdownDate - now;
// 计算剩余时间
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// 显示倒计时
document.getElementById("countdown").innerHTML = days + " 天 " + hours + " 小时 "
+ minutes + " 分钟 " + seconds + " 秒 ";
// 倒计时结束时触发烟花效果
if (distance < 0) {
clearInterval(countdown);
showFireworks();
}
}, 1000);
// 显示烟花效果
function showFireworks() {
var canvas = document.getElementById("fireworks");
var ctx = canvas.getContext("2d");
var particles = [];
var particleCount = 100;
var particleSpeed = 5;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function Particle() {
this.x = canvas.width / 2;
this.y = canvas.height;
this.vx = Math.random() * particleSpeed - particleSpeed / 2;
this.vy = Math.random() * -particleSpeed;
this.gravity = 0.1;
this.opacity = Math.random() * 0.5 + 0.5;
this.color = getRandomColor();
}
Particle.prototype.update = function() {
this.x += this.vx;
this.y += this.vy + this.gravity;
this.opacity -= 0.01;
if (this.opacity <= 0) {
particles.splice(particles.indexOf(this), 1);
}
};
Particle.prototype.draw = function() {
ctx.globalAlpha = this.opacity;
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, 2, 2);
};
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function createParticles() {
for (var i = 0; i < particleCount; i++) {
var particle = new Particle();
particles.push(particle);
}
}
function updateParticles() {
for (var i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
createParticles();
updateParticles();
requestAnimationFrame(animate);
}
animate();
}
</script>
</body>
</html>
```
这段代码会在页面上显示一个倒计时,当倒计时结束时,会触发烟花效果。你可以将这段代码保存为一个HTML文件,然后在浏览器中打开查看效果。
阅读全文