css钟摆制作
CSS钟摆制作方法
使用CSS动画和关键帧可以实现钟摆效果,以下是具体实现步骤:
HTML结构

<div class="pendulum">
<div class="string"></div>
<div class="ball"></div>
</div>
CSS样式
.pendulum {
position: relative;
width: 2px;
height: 200px;
margin: 0 auto;
transform-origin: top center;
animation: swing 2s infinite alternate ease-in-out;
}
.string {
width: 2px;
height: 150px;
background: #333;
margin: 0 auto;
}
.ball {
width: 30px;
height: 30px;
border-radius: 50%;
background: #ff5722;
position: absolute;
bottom: 0;
left: -14px;
}
@keyframes swing {
0% {
transform: rotate(30deg);
}
100% {
transform: rotate(-30deg);
}
}
优化钟摆效果
为了让钟摆运动更自然,可以调整动画时间和缓动函数:

animation: swing 1.5s infinite alternate cubic-bezier(0.37, 0, 0.63, 1);
添加物理效果
模拟真实钟摆的重力加速度效果:
@keyframes swing {
0%, 100% {
transform: rotate(20deg);
animation-timing-function: cubic-bezier(0.5, 0.05, 0.8, 0.5);
}
50% {
transform: rotate(-20deg);
animation-timing-function: cubic-bezier(0.2, 0.5, 0.5, 1);
}
}
多钟摆组合
创建多个同步摆动的钟摆:
.pendulum:nth-child(1) {
animation-delay: 0s;
}
.pendulum:nth-child(2) {
animation-delay: 0.2s;
}
.pendulum:nth-child(3) {
animation-delay: 0.4s;
}
这些方法可以创建出不同风格的CSS钟摆效果,通过调整参数可以获得不同的摆动速度和幅度。






