|
|

楼主 |
发表于 2022-2-26 22:42
|
显示全部楼层
一些说明:
HTML代码很简单,就一行:
<div id="circleBox"></div>
CSS代码是一个元素带两个伪元素:
一、元素代码
#circleBox {
margin: auto; /* 居中 */
margin-top: 100px; /* 上方留点空间 */
width: 100px; /* 宽度 */
height: 100px; /* 高度 */
border-radius: 50%; /* 圆形外观 */
background: tomato; /* 背景色 */
padding: 10px; /* 内边距 :作用是模拟轨道 */
position: relative; /* 相对定位 :必须 */
}
二、伪元素共性代码:
#circleBox::before, #circleBox::after {
content: ""; /* 必须 :伪元素能显示的依赖 */
position: absolute; /* 绝对定位 :必须 */
}
三、遮罩层伪元素代码:
#circleBox::before {
/* 高宽与父盒子一致,父盒子有padding值,padding部分不会被遮罩 */
width: 100px; /* 宽度 */
height: 100px; /* 高度 */
border-radius: 50%;
background: #ddd; /* 背景色 */
}
四、小球伪元素代码:
#circleBox::after {
width: 10px; /* 宽度 */
height: 10px; /* 高度 */
border-radius: 50%;
background: darkgreen;
top: 23px; /* 移位令其进入轨道 */
transform-origin: 50px 37px; /* 运行半径 :根据移位计算 */
animation: cRt 5s linear infinite; /* 运行动画 */
}
这一部分,如果不移位,则运行半径应当为 50px 50px,则小球处在轨道的外边缘,不能进入轨道,故而适当移位。移位的方法很多,处于简洁,仅移了垂直方向的位置,如此,运行半径就比较好掌握,仅变化y轴的值。
五、动画代码:
@keyframes cRt {
to { transform: rotate(360deg); }
}
动画部分非常简单,就是转转360度,因为小球有运行半径,所以它不是在原地转动,而是绕着轨道旋转。
|
|