CSS 星空流星雨是一种网页特效,通过 CSS 动画和 JavaScript 实现。它模拟了夜空中流星划过的效果,通常用于网站的背景或装饰元素,增加视觉效果和用户体验。
以下是一个简单的 CSS 星空流星雨的实现示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS 星空流星雨</title>
<style>
body {
margin: 0;
overflow: hidden;
background: black;
}
.star {
position: absolute;
width: 2px;
height: 2px;
background: white;
border-radius: 50%;
}
.meteor {
position: absolute;
width: 2px;
height: 20px;
background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
border-radius: 50%;
animation: fall 3s linear infinite;
}
@keyframes fall {
from {
transform: translateY(-20px) rotate(0deg);
}
to {
transform: translateY(100vh) rotate(360deg);
}
}
</style>
</head>
<body>
<script>
function createStar() {
const star = document.createElement('div');
star.classList.add('star');
star.style.left = `${Math.random() * 100}vw`;
star.style.top = `${Math.random() * 100}vh`;
document.body.appendChild(star);
}
function createMeteor() {
const meteor = document.createElement('div');
meteor.classList.add('meteor');
meteor.style.left = `${Math.random() * 100}vw`;
meteor.style.top = `${Math.random() * 100}vh`;
meteor.style.animationDuration = `${Math.random() * 3 + 2}s`;
document.body.appendChild(meteor);
setTimeout(() => meteor.remove(), 3000);
}
setInterval(createStar, 100);
setInterval(createMeteor, 500);
</script>
</body>
</html>requestAnimationFrame 优化动画性能。希望这些信息对你有所帮助!