先上效果图
通过一个父div和两个子div即可实现,主要考虑三点
- 两个子
div
重叠在一起设置父元素
position: relative
,然后设置子元素position: absolute
就可以使子元素的div重合,然后配合left: calc(20% - 3px)
使两个子元素的div居中 - 子
div
的border
的设置使用
border-radius: 50%
使之成为圆形,然后使用border-top-color: transparent
造成一个缺口 - 设置两个子div的动画或者父div的动画
通过CSS的动画属性设置即可,可以使用贝塞尔曲线设置两个不同的
animation-timing-function
,当然只设置父元素的动画也是可以的
代码如下所示:
<!DOCTYPE html>
<head>
<style>
@keyframes anim {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.loadingBox {
width: 80px;
height: 80px;
padding: 0px;
left: calc(50% - 80px);
top: calc(50% - 80px);
position: absolute;
}
.outerBox {
left: calc(10% - 3px);
top: calc(10% - 3px);
width: 80%;
height: 80%;
position: absolute;
border-radius: 50%;
border: medium cornflowerblue solid;
border-right-color: transparent;
animation: anim 847ms cubic-bezier(.42, .61, .58, .41) infinite;
}
.innerBox {
left: calc(20% - 3px);
top: calc(20% - 3px);
width: 60%;
height: 60%;
position: absolute;
border-radius: 50%;
border: 3px cornflowerblue solid;
border-top-color: transparent;
animation: anim 971ms cubic-bezier(.42, .61, .58, .41) infinite;
}
</style>
</head>
<body style="padding: 20px;background-color: whitesmoke;">
<div class="loadingBox" id="box">
<div class="innerBox"></div>
<div class="outerBox"></div>
</div>
</body>
</html>