前言
Hello!小伙伴!
非常感謝您閱讀海轟的文章,倘若文中有錯誤的地方,歡迎您指出~
?
自我介紹 ?(?ˊ?ˋ)?
昵稱:海轟
標簽:程序猿|C++選手|學(xué)生
簡介:因C語言結(jié)識編程,隨后轉(zhuǎn)入計算機專業(yè),有幸拿過國獎、省獎等,已保研。目前正在學(xué)習(xí)C++/Linux(真的真的太難了~)
學(xué)習(xí)經(jīng)驗:扎實基礎(chǔ) + 多做筆記 + 多敲代碼 + 多思考 + 學(xué)好英語!
?
<font color="red" font-wight="800">【動畫消消樂】</font> 平時學(xué)習(xí)生活比較枯燥,無意之間對一些網(wǎng)頁、應(yīng)用程序的過渡/加載動畫產(chǎn)生了濃厚的興趣,想知道具體是如何實現(xiàn)的? 便在空閑的時候?qū)W習(xí)下如何使用css實現(xiàn)一些簡單的動畫效果,文章僅供作為自己的學(xué)習(xí)筆記,記錄學(xué)習(xí)生活,爭取理解動畫的原理,多多“消滅”動畫!
效果展示
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: #222f3e;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid white;
}
span {
width: 20px;
height: 12px;
display: inline-block;
position: relative;
border-radius: 4px;
background: white;
animation: loading 0.6s 0.3s ease infinite alternate;
}
span::before, span::after {
content: '';
width: 20px;
height: 12px;
background: white;
position: absolute;
border-radius: 4px;
top: 0;
right: 110%;
animation: loading 0.6s ease infinite alternate;
}
span::after {
left: 110%;
right: auto;
animation-delay: 0.6s;
}
@keyframes loading {
0% {
width: 20px;
}
100% {
width: 48px;
}
}
原理詳解
步驟1
使用一個span標簽
<span></span>
設(shè)置為
- 寬度為20px 高度為12px
- 相對定位
- 背景顏色:白色
- border-radius: 4px;
span {
width: 20px;
height: 12px;
position: relative;
border-radius: 4px;
background: white;
}
效果圖如下:
步驟2
使用span::before、span::after
同時設(shè)置為:
- 寬度為20px 高度為12px
- 背景色為白色
- 絕對定位
( top: 0; right: 110%;) - border-radius: 4px;
span::before, span::after {
content: '';
width: 20px;
height: 12px;
background: white;
position: absolute;
border-radius: 4px;
top: 0;
right: 110%;
}
效果圖如下:
步驟3
再單獨設(shè)置span::after
使其與before分離 位于span右邊
- 位置為:
left: 110%; - 背景色為紅色(便于區(qū)分before)
span::after {
left: 110%;
background-color: red;
}
效果圖如下:
步驟4
為span添加動畫
只需要設(shè)置 span寬度一個屬性變化 即可,效果描述為:
- 初始位置:
width: 20px; - 末尾位置:
width: 48px;
@keyframes loading {
0% {
width: 20px;
}
100% {
width: 48px;
}
}
span使用該動畫
span {
animation: loading 0.6s ease infinite alternate;
}
效果圖如下:
步驟5
對span::before、span::after使用同樣的動畫
從位置關(guān)系上來說
從左到右依次是:
span::before、span、span::after
為了使得動畫有一定的錯落感
分別設(shè)置動畫開始延遲時間為0s 0.3s 0.6s(與位置相對應(yīng))
span::before, span::after {
animation: loading 0.6s ease infinite alternate;
}
span {
animation: loading 0.6s 0.3s ease infinite alternate;
}
span::after {
animation-delay: 0.6s;
}
效果如下:
步驟6
最后再注釋掉span::after的背景顏色紅色即可
span::after {
background-color: red;
}
得到最終的效果
結(jié)語
文章僅作為學(xué)習(xí)筆記,記錄從0到1的一個過程
希望對您有所幫助,如有錯誤歡迎小伙伴指正~
我是 <font color="#0984e3">海轟?(?ˊ?ˋ)?</font>
如果您覺得寫得可以的話,請點個贊吧
謝謝支持??