<style>
/*定义齿轮的样式*/
.gear {
width: 100px;
height: 100px;
border-radius: 50%;
border: 10px solid #ccc;
position: relative;
margin: 50px auto;
box-shadow: inset -3px -3px 6px rgba(0,0,0,0.2),
inset 3px 3px 6px rgba(255,255,255,0.7),
3px 3px 6px rgba(0,0,0,0.2);
}
.gear:before, .gear:after {
content: "";
position: absolute;
border-radius: 50%;
background: #ccc;
}
.gear:before {
width: 25px;
height: 25px;
top: 8%;
left: 8%;
}
.gear:after {
width:25px;
height: 25px;
top: 8%;
left: 65%;
}
/* 定义转动动画 */
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
/* 应用动画到齿轮上 */
.gear {
animation-name: rotate;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
</style>
<p>齿轮是机械的核心组件,如何用CSS3实现齿轮转动动画特效呢?以下是一个简单的示例:</p>
<div class="gear"></div>
<p>首先,我们定义了一个齿轮的样式,包括宽度、高度、边框、阴影等等。其中,齿轮上有两个半径为25px的小圆,模拟了真实齿轮的齿轮轴。</p>
<p>接下来,我们通过CSS3的keyframes关键字定义了一个rotate动画,从0度旋转到360度,动画持续时间为1秒,无限循环,线性动画变化。</p>
<p>最后,我们将动画应用到齿轮上,通过animation-name、animation-duration、animation-iteration-count、animation-timing-function属性来控制动画效果。</p>