css钟摆制作
CSS钟摆制作方法
使用CSS动画和关键帧可以实现钟摆效果。以下是具体实现步骤:
HTML结构
<div class="pendulum">
<div class="string"></div>
<div class="weight"></div>
</div>
CSS样式
.pendulum {
position: relative;
width: 200px;
height: 300px;
margin: 50px auto;
}
.string {
position: absolute;
width: 2px;
height: 200px;
background: #333;
left: 50%;
transform-origin: top center;
animation: swing 2s infinite ease-in-out alternate;
}
.weight {
position: absolute;
width: 40px;
height: 40px;
background: #ff5722;
border-radius: 50%;
bottom: 0;
left: 50%;
margin-left: -20px;
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}
@keyframes swing {
0% {
transform: rotate(-30deg);
}
100% {
transform: rotate(30deg);
}
}
进阶优化技巧
增加物理感更强的钟摆效果可以通过调整动画曲线:
@keyframes swing {
0%, 100% {
transform: rotate(-30deg);
animation-timing-function: ease-out;
}
50% {
transform: rotate(30deg);
animation-timing-function: ease-in;
}
}
添加悬挂点装饰:
.pendulum::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
background: #333;
border-radius: 50%;
top: 0;
left: 50%;
margin-left: -10px;
}
响应式调整
使钟摆大小适应不同屏幕:
@media (max-width: 600px) {
.pendulum {
transform: scale(0.7);
}
}
通过调整@keyframes中的角度值和动画持续时间,可以改变钟摆摆动的幅度和速度。增加transform-origin属性确保绳子从顶部中心点正确摆动。







