以下是一个简单的HTML页面示例,使用HTML、CSS和JavaScript来创建一个七夕情人节表白页面,其中包含星空背景和随机飘落的萤火虫动画。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>七夕情人节表白</title>
<style>
body, html {
margin: 0;
padding: 0;
height: 100%;
}
.stars {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 1;
}
.firefly {
width: 3px;
height: 3px;
background: #F0DB4F;
border-radius: 50%;
position: absolute;
z-index: 2;
}
</style>
</head>
<body>
<div class="stars"></div>
<script>
const canvas = document.createElement('canvas');
canvas.classList.add('stars');
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const ctx = canvas.getContext('2d');
const fireflies = [];
function Firefly() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 1.5;
this.speedX = (Math.random() - 0.5) * 5;
this.speedY = (Math.random() - 0.5) * 5;
this.light = Math.random() * 0.9;
}
Firefly.prototype.draw = function() {
ctx.save();
ctx.globalAlpha = this.light;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fillStyle = '#F0DB4F';
ctx.fill();
ctx.restore();
};
Firefly.prototype.update = function() {
this.x += this.speedX;
this.y += this.speedY;
if (this.x > canvas.width || this.x < 0) this.speedX *= -1;
if (this.y > canvas.height || this.y < 0) this.speedY *= -1;
this.draw();
};
function init() {
canvas.addEventListener('mousemove', (e) => {
fireflies.push(new Firefly());
fireflies[fireflies.length - 1].x = e.clientX;
fireflies[fireflies.length - 1].y = e.clientY;
});
setInterval(animate, 1000 / 60);
}
function animate() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
fireflies.forEach((firefly) => {
firefly.update();
});
}
init();
</script>
</body>
</html>
这段代码创建了一个全屏的HTML页面,其中包含一个固定位置的canvas元素,用于绘制星空背景。鼠标在页面上移动时,会产生新的萤火虫,随机飘落在页面上。背景的星空和飘落的萤火虫都是通过JavaScript动态生