用css制作月食
使用CSS制作月食效果
月食效果可以通过CSS的box-shadow和border-radius属性结合动画来实现。以下是具体实现方法:
HTML结构
创建一个简单的HTML元素作为月亮:

<div class="moon"></div>
CSS样式
通过CSS为月亮添加阴影和动画效果:

.moon {
width: 100px;
height: 100px;
background: #f5d742;
border-radius: 50%;
position: relative;
box-shadow: 0 0 20px #f5d742;
animation: eclipse 8s infinite;
}
月食动画
使用box-shadow模拟月食过程:
@keyframes eclipse {
0% {
box-shadow: 0 0 20px #f5d742;
}
50% {
box-shadow: -50px 0 0 0 #222, 0 0 20px #f5d742;
}
100% {
box-shadow: 0 0 20px #f5d742;
}
}
完整示例
将HTML和CSS组合起来:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.moon {
width: 100px;
height: 100px;
background: #f5d742;
border-radius: 50%;
position: relative;
box-shadow: 0 0 20px #f5d742;
animation: eclipse 8s infinite;
}
@keyframes eclipse {
0% {
box-shadow: 0 0 20px #f5d742;
}
50% {
box-shadow: -50px 0 0 0 #222, 0 0 20px #f5d742;
}
100% {
box-shadow: 0 0 20px #f5d742;
}
}
</style>
</head>
<body>
<div class="moon"></div>
</body>
</html>
效果说明
- 初始状态显示完整的黄色月亮
- 动画中间阶段显示黑色阴影从左向右覆盖月亮
- 最后阶段恢复完整月亮
- 动画循环展示月食全过程
可以通过调整box-shadow的偏移量和颜色来改变月食的效果和速度。






