效果展示 - 1
在這里插入圖片描述
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: #ed556a;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid white;
}
span {
width: 48px;
height: 48px;
display: inline-block;
position: relative;
background: white;
animation: loading 3s linear infinite;
}
@keyframes loading {
/*先x軸翻轉(zhuǎn)180度 后y軸翻轉(zhuǎn)180度*/
0% {
transform: perspective(200px) rotateX(0deg) rotateY(0deg);
}
50% {
transform: perspective(200px) rotateX(-180deg) rotateY(0deg);
}
100% {
transform: perspective(200px) rotateX(-180deg) rotateY(-180deg)
}
}
原理詳解
步驟1
使用span標簽攒射,設(shè)置為
- 寬度、高度均為48px
- 背景色:白色
width: 48px;
height: 48px;
background: white;
效果圖如下
在這里插入圖片描述
步驟2
為span添加動畫
- 先繞x軸翻轉(zhuǎn)180度
- 后繞y軸翻轉(zhuǎn)180度
@keyframes loading {
/*先x軸翻轉(zhuǎn)180度 后y軸翻轉(zhuǎn)180度*/
0% {
transform: perspective(200px) rotateX(0deg) rotateY(0deg);
}
50% {
transform: perspective(200px) rotateX(-180deg) rotateY(0deg);
}
100% {
transform: perspective(200px) rotateX(-180deg) rotateY(-180deg)
}
}
效果圖如下
在這里插入圖片描述
效果展示 - 2
在這里插入圖片描述
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: #ed556a;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid white;
}
span {
width: 48px;
height: 96px;
display: inline-block;
position: relative;
color: white;
border: 3px solid;
animation: loading 2s linear infinite alternate;
}
span::before {
content: '';
position: absolute;
top: -15px;
left: 6px;
width: 36px;
height: 12px;
background-color: white;
}
@keyframes loading {
0% {
box-shadow: 0 0 inset
}
100% {
box-shadow: 0 -96px inset
}
}
原理詳解
步驟1
使用span標簽恒水,設(shè)置為
- 相對定位
- 寬度48px会放,高度96px
- 邊框:3px solid 白色
- color:白色
span {
width: 48px;
height: 96px;
position: relative;
color: white;
border: 3px solid;
}
效果圖如下
在這里插入圖片描述
步驟2
利用span::before偽元素,充當最上方白色方塊
設(shè)置為
- 絕對定位(top -15px钉凌,left 6px)
- 寬度:36px
- 高度:12px
- 背景色:白色
span::before {
content: '';
position: absolute;
top: -15px;
left: 6px;
width: 36px;
height: 12px;
background-color: white;
}
效果圖如下
在這里插入圖片描述
注:
- top是-15px咧最,是因為上方白色方塊寬度12px,還需要加上下方白色邊框3px,一共12+3=15px
- left為6px矢沿,是因為要使上方白色方塊居中滥搭,需要向左移動(48-36)/2=6px
步驟3
使用span的陰影(box-shadow)作為動畫
需要實現(xiàn)的效果:逐漸填充下方白色部分內(nèi)部
- 陰影向內(nèi)
以動畫中的幾幀說明:
當陰影向內(nèi)延伸10px時
span {
box-shadow: 0 -10px inset
}
效果圖如下
在這里插入圖片描述
當陰影向內(nèi)延伸48px時
span {
box-shadow: 0 -48px inset
}
效果圖如下
在這里插入圖片描述
當陰影向內(nèi)延伸96px時
span {
box-shadow: 0 -96px inset
}
效果圖如下
在這里插入圖片描述
綜上:陰影向內(nèi)從0延伸96px,重復(fù)即可
animation: loading 2s linear infinite ;
@keyframes loading {
0% {
box-shadow: 0 0 inset
}
100% {
box-shadow: 0 -96px inset
}
}
效果圖如下
在這里插入圖片描述
步驟4
設(shè)置動畫交替進行
animation: loading 2s linear infinite alternate;
效果圖如下
在這里插入圖片描述