「HTML+CSS」--自定义加载动画【013】
效果展示
在这里插入图片描述
Demo代码
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<section><span></span></section>
</body>
</html>
CSS
html,body{
margin: 0;
height: 100%;
}
body{
display: flex;
justify-content: center;
align-items: center;
background: #263238;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
/* 红色边框仅作提示 */
border: 2px solid red;
}
span{
width : 96px;
height: 96px;
border: 2px solid red;
border-radius: 50%;
position: relative;
display: flex;
justify-content: center;
animation: rotation 2s linear infinite;
}
span::before{
position: relative;
content: '';
width: 24px;
height: 24px;
background-color: rebeccapurple;
border: 2px solid white;
border-radius: 50%;
}
@keyframes rotation {
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg)
}
}
原理详解
步骤1
使用span标签,生成一个正方形
- 宽度、高度均为96px
- 边框:2px solid 红色
width : 96px;
height: 96px;
border: 2px solid red;
效果图如下
步骤2
span标签采用flex布局
display: flex;
justify-content: center;/*水平居中*/
span::before伪类作为小球
- 位于正方形上边框中间
- 宽度、高度均为24px
- 边框:2px solid white
- 背景色: rebeccapurple(紫色)
效果图如下
步骤3
span、span::before圆角化
border-radius: 50%;
效果图如下
步骤4
添加旋转动画
animation: rotation 2s linear infinite;
/*动画实现*/
@keyframes rotation {
0% { transform: rotate(0deg) }
100% { transform: rotate(360deg)
}
}
效果图如下
在这里插入图片描述