前言

Hello!小夥伴!

非常感謝您閱讀海轟的文章,倘若文中有錯誤的地方,歡迎您指出~

 

自我介紹 ଘ(੭ˊᵕˋ)੭

暱稱:海轟

標籤:程式猿|C++選手|學生

簡介:因C語言結識程式設計,隨後轉入計算機專業,有幸拿過國獎、省獎等,已保研。目前正在學習C++/Linux(真的真的太難了~)

學習經驗:紮實基礎 + 多做筆記 + 多敲程式碼 + 多思考 + 學好英語!

 

【動畫消消樂】 平時學習生活比較枯燥,無意之間對一些網頁、應用程式的過渡/載入動畫產生了濃厚的興趣,想知道具體是如何實現的? 便在空閒的時候學習下如何使用css實現一些簡單的動畫效果,文章僅供作為自己的學習筆記,記錄學習生活,爭取理解動畫的原理,多多“消滅”動畫!

效果展示

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>
<div class="circle">
<div class="wave"></div>
</div>
</section>
</body> </html>

CSS

/*
模版css樣式
僅供演示使用
*/ html, body {
margin: 0;
height: 100%;
} body {
display: flex;
justify-content: center;
align-items: center;
background-color: #12383e;
} section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border-radius: 20px;
border: 18px solid white;
overflow: hidden;
} section::before {
content: 'CSS';
width: 150px;
height: 150px;
text-align: center;
line-height: 250px;
background-color: #00cec9;
position: absolute;
top: -76px;
right: -76px;
transform: translate(50%, -50%);
font-size: 32px;
font-weight: 500;
font-family: sans-serif;
color: white;
transform: rotate(45deg);
} section::after {
content: '';
position: absolute;
width: 100%;
height: 100%;
border: 10px solid white;
border-radius: 20px;
} /* 實現程式碼 */ .circle {
position: relative;
width: 200px;
height: 200px;
background: #b0f4ff;
border-radius: 50%;
overflow: hidden;
animation: loadingBreath 5s infinite linear;
} .circle::before {
content: 'Loading...';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 18px;
letter-spacing: 2px;
color: #10a789;
font-family: sans-serif;
z-index: 2;
} .circle::after {
content: '';
position: absolute;
width: 100%;
height: 25%;
bottom: 0;
background-image: linear-gradient(to top, #12c8e0, #36e9ff, #5ffbf1);
animation: loadingRun 5s linear infinite;
} .wave::before {
content: '';
position: absolute;
left: -50%;
width: 200%;
height: 200%;
z-index: 1;
background-color: #85f7fb;
border-radius: 52% 25% 62% 69%/25% 38%;
animation: loadingWave 5s linear infinite;
} .wave::after {
content: '';
position: absolute;
left: -50%;
width: 200%;
height: 200%;
z-index: 1;
background-color: #d0f4ff;
border-radius: 42% 38% 40% 62%/28% 35%;
animation: loadingWave 5s ease-in-out infinite;
} /* 呼吸燈動畫 */ @keyframes loadingBreath {
0% {
box-shadow: 0 0 5px 0 #85f7fb;
}
25% {
box-shadow: 0 0 20px 0 #85f7fb;
}
50% {
box-shadow: 0 0 5px 0 #85f7fb;
}
75% {
box-shadow: 0 0 20px 0 #85f7fb;
}
100% {
box-shadow: 0 0 5px 0 #85f7fb;
}
} /* 底部液體上升動畫 */ @keyframes loadingRun {
0% {
height: 25%;
}
100% {
height: 100%;
}
} /* wave動畫 */ @keyframes loadingWave {
0% {
top: -100%;
transform: rotate(0);
}
100% {
top: -200%;
transform: rotate(360deg);
}
}

原理詳解

步驟1

從效果圖上分析

可以將其分為兩個部分

  • 容器
  • 波浪

這裡使用兩個div,一個為circle類,一個為wave類,分別代表容器和wave

            <div class="circle">
<div class="wave"></div>
</div>

步驟2

設定circle類

  • 相對定位
  • 寬度、高度均為200px
  • 背景色:#b0f4ff
  • 圓角:50%
.circle {
position: relative;
width: 200px;
height: 200px;
background: #b0f4ff;
border-radius: 50%;
}

效果圖如下:

步驟3

利用.circle::befor偽元素

用於顯示“Loading...”字樣

設定為

  • 絕對定位
  • 使其位於正中間( top: 50%; left: 50%; transform: translate(-50%, -50%);)
  • 字型大小:18px
  • 顏色:#10a789;
  • z-index:2(比1大就行 使其文字處於最上層)
.circle::before {
content: 'Loading...';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 18px;
letter-spacing: 2px;
color: #10a789;
font-family: sans-serif;
z-index: 2;
}

效果圖如下:

步驟4

利用.circle::after偽元素

設定為

  • 絕對定位(bottom: 0; )
  • 寬度:100%
  • 高度:25%
  • 背景顏色為漸變色 linear-gradient(to top, #12c8e0, #36e9ff, #5ffbf1);
.circle::after {
content: '';
position: absolute;
width: 100%;
height: 25%;
bottom: 0;
background-image: linear-gradient(to top, #12c8e0, #36e9ff, #5ffbf1);
}

效果圖如下:

步驟5

為.circle::after偽元素新增動畫

使其隨時間其高度逐漸增大

只需要明確兩個關鍵幀

  • 初始位置:height: 25%
  • 結束位置:height: 100%
.circle::after {
animation: loadingRun 5s linear infinite;
} @keyframes loadingRun {
0% {
height: 25%;
}
100% {
height: 100%;
}
}

效果圖如下:

步驟6

對circle設定隱藏溢位

.circle {
overflow: hidden;
}

效果圖如下:

步驟7

這裡先註釋circle隱藏溢位 以及 circle::after動畫 便於後面單獨分析

.circle {
/* overflow: hidden; */
}
.circle::after {
/* animation: loadingRun 5s linear infinite; */
}

然後我們使用wave的兩個偽元素.wave::before、.wave::afte與cirle::after產生波浪的效果

首先設定wave::before

  • 絕對定位(left: -50%;)
  • 寬度、高度均為200%
  • z-index:1
  • 背景色:#85f7fb
  • border-radius: 52% 25% 62% 69%/25% 38%; 重點
.wave::before {
content: '';
position: absolute;
left: -50%;
width: 200%;
height: 200%;
z-index: 1;
background-color: #85f7fb;
border-radius: 52% 25% 62% 69%/25% 38%;/*重點*/
}

效果圖如下:



注:.wave::before z-index為1 大於circile(0) 小於.circle::before(2)

為.wave::before 新增動畫

效果描述

自身不斷旋轉的同時 也不斷上升

.wave::before {
animation: loadingWave 5s linear infinite;
} @keyframes loadingWave {
0% {
top: -100%;
transform: rotate(0);
}
100% {
top: -200%;
transform: rotate(360deg);
}
}

效果圖如下:

同理,對wave::after進行同樣的設定

不同在於ç四邊圓角率與before不同、顏色淺一點

border-radius: 42% 38% 40% 62%/28% 35%;
background-color: #d0f4ff;

其他都一樣



當wave::after、before運用同樣的動畫時

效果圖如下:

步驟8

取消circle隱藏溢位 以及 circle::after動畫

.circle {
overflow: hidden;
}
.circle::after {
animation: loadingRun 5s linear infinite;
}

效果圖如下:

步驟9

最後為cirlce新增一個呼吸燈動畫效果

.circle {
animation: loadingBreath 5s infinite linear;
} ```css @keyframes loadingBreath {
0% {
box-shadow: 0 0 5px 0 #85f7fb;
}
25% {
box-shadow: 0 0 20px 0 #85f7fb;
}
50% {
box-shadow: 0 0 5px 0 #85f7fb;
}
75% {
box-shadow: 0 0 20px 0 #85f7fb;
}
100% {
box-shadow: 0 0 5px 0 #85f7fb;
}
}

得到最終效果圖

結語

文章僅作為學習筆記,記錄從0到1的一個過程

希望對您有所幫助,如有錯誤歡迎小夥伴指正~

我是 海轟ଘ(੭ˊᵕˋ)੭

如果您覺得寫得可以的話,請點個贊吧

謝謝支援️

學習參考:

https://www.bilibili.com/video/BV1Ai4y1t7od

https://developer.mozilla.org/zh-CN/docs/Web/CSS/::before