CSS星星动画是一种使用CSS(层叠样式表)创建的视觉效果,通常用于网页设计中的背景装饰或加载动画。这种动画通过CSS的动画属性(如@keyframes)来实现星星的闪烁效果。
以下是一个简单的CSS星星闪烁动画的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Star Animation</title>
<style>
.star {
position: absolute;
width: 2px;
height: 2px;
background-color: white;
border-radius: 50%;
animation: twinkle 2s infinite ease-in-out;
}
@keyframes twinkle {
0%, 100% {
opacity: 0.5;
transform: scale(1);
}
50% {
opacity: 1;
transform: scale(1.2);
}
}
body {
margin: 0;
overflow: hidden;
background-color: black;
}
</style>
</head>
<body>
<div class="star" style="left: 10%; top: 20%;"></div>
<div class="star" style="left: 30%; top: 50%;"></div>
<div class="star" style="left: 60%; top: 80%;"></div>
<div class="star" style="left: 80%; top: 40%;"></div>
</body>
</html>@keyframes中的动画时间设置不均匀。@keyframes中的时间点,确保动画的各个阶段时间分配均匀。position: absolute结合随机数生成位置。will-change属性优化动画性能。通过以上方法,可以有效地创建和优化CSS星星动画,提升网页的视觉效果和用户体验。