前言
Hello!小伙伴从诲!
非常感謝您閱讀海轟的文章蝶缀,倘若文中有錯(cuò)誤的地方裳凸,歡迎您指出~
?
自我介紹 ?(?ˊ?ˋ)?昵稱:海轟
標(biāo)簽:程序猿|C++選手|學(xué)生
簡(jiǎn)介:因C語(yǔ)言結(jié)識(shí)編程,隨后轉(zhuǎn)入計(jì)算機(jī)專業(yè)缤至,有幸拿過國(guó)獎(jiǎng)潮罪、省獎(jiǎng)等康谆,已保研。目前正在學(xué)習(xí)C++/Linux(真的真的太難了~)
學(xué)習(xí)經(jīng)驗(yàn):扎實(shí)基礎(chǔ) + 多做筆記 + 多敲代碼 + 多思考 + 學(xué)好英語(yǔ)嫉到!
?
<font color="red" font-wight="800">【動(dòng)畫消消樂】</font> 平時(shí)學(xué)習(xí)生活比較枯燥沃暗,無(wú)意之間對(duì)一些網(wǎng)頁(yè)、應(yīng)用程序的過渡/加載動(dòng)畫產(chǎn)生了濃厚的興趣何恶,想知道具體是如何實(shí)現(xiàn)的孽锥? 便在空閑的時(shí)候?qū)W習(xí)下如何使用css實(shí)現(xiàn)一些簡(jiǎn)單的動(dòng)畫效果,文章僅供作為自己的學(xué)習(xí)筆記细层,記錄學(xué)習(xí)生活惜辑,爭(zhēng)取理解動(dòng)畫的原理,多多“消滅”動(dòng)畫疫赎!
效果展示
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;
/* background-color: #82466e; */
animation: backColor 4s infinite;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid white;
}
span {
width: 5px;
height: 5px;
display: inline-block;
position: relative;
border-radius: 4px;
color: white;
background: currentColor;
animation: animloader66 0.6s 0.6s linear infinite alternate;
}
span::before, span::after {
content: '';
width: 5px;
height: 5px;
border-radius: 4px;
background: currentColor;
position: absolute;
left: 0;
top: 15px;
animation: animloader66 0.6s 0.9s linear infinite alternate;
}
[data-index="66"] {
justify-content: normal;
padding-left: 15%;
}
span::after {
top: -15px;
animation-delay: 0s;
}
@keyframes animloader66 {
0% {
width: 5px;
}
100% {
width: 48px;
}
}
原理詳解
步驟1
使用span標(biāo)簽盛撑,設(shè)置為
- 相對(duì)定位
- 寬度、高度均為5px
- 背景色:白色
- color:白色
- border-radius: 4px
span {
width: 5px;
height: 5px;
position: relative;
border-radius: 4px;
color: white;
background: white;
}
效果圖如下
步驟2
為span添加動(dòng)畫
動(dòng)畫描述效果為:span寬度從5px變化到48px捧搞,再回到5px
span {
animation: loading 0.6s 0s linear infinite alternate;
}
@keyframes loading {
0% {
width: 5px;
}
100% {
width: 48px;
}
}
效果圖如下
注:span事先是設(shè)置固定在頁(yè)面正中的抵卫,所以才會(huì)出現(xiàn)上圖效果
步驟3
使用span的兩個(gè)偽元素:span::before、span::after
同時(shí)設(shè)置為
- 絕對(duì)定位( left: 0 top: 15px)
- 寬度胎撇、高度均為5px
- 背景色:白色
- border-radius: 4px
span::before, span::after {
content: '';
width: 5px;
height: 5px;
border-radius: 4px;
background: white;
position: absolute;
left: 0;
top: 15px;
}
位置關(guān)系如下
為span::before介粘、span::after添加同span一樣的動(dòng)畫,只是動(dòng)畫開始延遲時(shí)間設(shè)置為0.6s
span::before, span::after {
animation: loading 0.6s 0.6s linear infinite alternate;
}
效果圖如下:
注:可以發(fā)現(xiàn)创坞,span和它的兩個(gè)偽元素的左部分一直是處于同一條直線上
步驟4
分離span::before碗短、span::after
將span::after移動(dòng)至位于span上方15px處 且動(dòng)畫延遲時(shí)間修改為0.9s
span::after {
top: -15px;
animation-delay: 0.9s;
}
得到最終的效果圖
結(jié)語(yǔ)
文章僅作為學(xué)習(xí)筆記,記錄從0到1的一個(gè)過程
希望對(duì)您有所幫助题涨,如有錯(cuò)誤歡迎小伙伴指正~
我是海轟?(?ˊ?ˋ)?偎谁,如果您覺得寫得可以的話,請(qǐng)點(diǎn)個(gè)贊吧
謝謝支持??