【動(dòng)畫消消樂(lè)】HTML+CSS 自定義加載動(dòng)畫 058
效果展示

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標(biāo)簽,設(shè)置為
相對(duì)定位 寬度48px,高度96px 邊框:3px solid 白色 color:白色
span {
width: 48px;
height: 96px;
position: relative;
color: white;
border: 3px solid;
}
效果圖如下

步驟2
利用span::before偽元素,充當(dāng)最上方白色方塊
設(shè)置為
絕對(duì)定位(top -15px,left 6px) 寬度:36px 高度:12px 背景色:白色
span::before {
content: '';
position: absolute;
top: -15px;
left: 6px;
width: 36px;
height: 12px;
background-color: white;
}
效果圖如下

注:
top是-15px,是因?yàn)樯戏桨咨綁K寬度12px,還需要加上下方白色邊框3px,一共12+3=15px left為6px,是因?yàn)橐股戏桨咨綁K居中,需要向左移動(dòng)(48-36)/2=6px
步驟3
使用span的陰影(box-shadow)作為動(dòng)畫
需要實(shí)現(xiàn)的效果:逐漸填充下方白色部分內(nèi)部
陰影向內(nèi)
以動(dòng)畫中的幾幀說(shuō)明:
當(dāng)陰影向內(nèi)延伸10px時(shí)
span {
box-shadow: 0 -10px inset
}
效果圖如下

當(dāng)陰影向內(nèi)延伸48px時(shí)
span {
box-shadow: 0 -48px inset
}
效果圖如下

當(dāng)陰影向內(nèi)延伸96px時(shí)
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è)置動(dòng)畫交替進(jìn)行
animation: loading 2s linear infinite alternate;
效果圖如下

結(jié)語(yǔ)
希望對(duì)您有所幫助
如有錯(cuò)誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
如果您覺(jué)得寫得可以的話
請(qǐng)點(diǎn)個(gè)贊吧
謝謝支持??
評(píng)論
圖片
表情
