3分鐘實(shí)現(xiàn)充電水波特效
點(diǎn)擊上方 前端瓶子君,關(guān)注公眾號(hào)
回復(fù)算法,加入前端編程面試算法每日一題群

來(lái)源:北極光之夜
https://juejin.cn/post/6967360136926986254
效果(源碼在最后):
這個(gè)效果跟水波加載動(dòng)畫 html+css是異曲同工之妙的。效果不難實(shí)現(xiàn),但是這個(gè)‘偷天換日’的用法是值得學(xué)習(xí)的。
實(shí)現(xiàn):
1. 定義標(biāo)簽,.container是底層盒子也就是電池外形,.water是其中的電量多少,.shadow是背后的陰影:
<div class="container">
<div class="water"></div>
<div class="shadow"></div>
</div>
復(fù)制代碼
2. 定義.container的基本樣式:
.container{
position: relative;
width: 200px;
height: 300px;
background-color: rgb(255, 255, 255);
border-radius: 10px;
box-shadow: 0 0 10px rgb(255, 255, 255) ;
}
復(fù)制代碼
position: relative; 相對(duì)定位。background-color: rgb(255, 255, 255); 白色。border-radius: 10px; 角弧度。box-shadow: 0 0 10px rgb(255, 255, 255) ; 陰影。
3.通過(guò)雙偽類定義電池的頭部:
.container::after{
content: '';
position: absolute;
top: -20px;
left: 50%;
width: 40px;
height: 20px;
transform: translateX(-50%);
background-color: rgb(255, 255, 255);
border-top-right-radius: 10px;
border-top-left-radius: 10px;
box-shadow: 0 0 10px rgb(255, 255, 255) ;
}
復(fù)制代碼
position: absolute; top: -20px; left: 50%; 定義位置 transform: translateX(-50%); 向左偏移自身的50%,目的是居中。4. 定義電量慢慢變多效果:
.water{
position: absolute;
bottom: 0;
width: 100%;
background-image: linear-gradient(0deg,rgb(9, 198, 245),rgb(44, 243, 120));
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
animation: rise 12s linear forwards;
overflow: hidden;
}
@keyframes rise{
0%{
height: 50px;
}
100%{
height: 80%;
filter: hue-rotate(360deg);
}
}
復(fù)制代碼
background-image: linear-gradient(0deg,rgb(9, 198, 245),rgb(44, 243, 120)); 漸變顏色。animation: rise 12s linear forwards; 定義動(dòng)畫,高度改變,相當(dāng)于充電。forwards是指定動(dòng)畫結(jié)束后保留最后一步的屬性。overflow: hidden; 溢出隱藏。filter: hue-rotate(360deg); 色相旋轉(zhuǎn),能使顏色改變。
5. 定義水波效果(原理是定義一個(gè)有弧度的白色盒子在轉(zhuǎn)動(dòng),其覆蓋掉.water的上面的一部分,.water再定義溢出隱藏,這樣就‘偷天換日’得到一個(gè)白色的波浪):
.water::after{
content: '';
position: absolute;
top: -370px;
left: -100px;
width: 400px;
height: 400px;
border-radius: 40%;
background-color: rgb(255, 255, 255);
animation: move 5s linear infinite;
}
@keyframes move{
100%{
transform: rotate(360deg);
}
}
復(fù)制代碼
位置大小和弧度可以看效果自己設(shè)定。transform: rotate(360deg); 旋轉(zhuǎn)。
6. 再定義一個(gè)水波,原理一樣,不過(guò)顏色要設(shè)置透明度,免得覆蓋掉另一個(gè)水波:
.water::before{
content: '';
position: absolute;
top: -360px;
left: -100px;
width: 400px;
height: 400px;
border-radius: 45%;
background-color: rgba(255, 255, 255,.5);
animation: move2 7s linear infinite;
}
@keyframes move2{
100%{
transform: rotate(360deg);
}
}
復(fù)制代碼
7. 定義背后的陰影,它的高度和顏色變換應(yīng)該和.water是一致的:
.shadow{
position: absolute;
bottom: -8px;
left: -3%;
width: 106%;
background-image: linear-gradient(0deg,rgb(9, 198, 245),rgb(44, 243, 120));
z-index: -1;
animation: bianse 12s linear forwards;
}
@keyframes bianse{
0%{
height: 50px;
filter: hue-rotate(0deg) blur(10px);
}
100%{
height: 80%;
filter: hue-rotate(360deg) blur(10px);
}
}
復(fù)制代碼
position: absolute; bottom: -8px; left: -3%; width: 106%;位置和大小。z-index: -1; 設(shè)置-1,顯示在最后。filter: hue-rotate(0deg) blur(10px); blur是模糊度。
完整代碼:
<!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">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: rgb(189, 189, 189);
}
.container{
position: relative;
width: 200px;
height: 300px;
background-color: rgb(255, 255, 255);
border-radius: 10px;
box-shadow: 0 0 10px rgb(255, 255, 255) ;
}
.container::after{
content: '';
position: absolute;
top: -20px;
left: 50%;
width: 40px;
height: 20px;
transform: translateX(-50%);
background-color: rgb(255, 255, 255);
border-top-right-radius: 10px;
border-top-left-radius: 10px;
box-shadow: 0 0 10px rgb(255, 255, 255) ;
}
.water{
position: absolute;
bottom: 0;
width: 100%;
background-image: linear-gradient(0deg,rgb(9, 198, 245),rgb(44, 243, 120));
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
animation: rise 12s linear forwards;
overflow: hidden;
}
@keyframes rise{
0%{
height: 50px;
}
100%{
height: 80%;
filter: hue-rotate(360deg);
}
}
.water::after{
content: '';
position: absolute;
top: -370px;
left: -100px;
width: 400px;
height: 400px;
border-radius: 40%;
background-color: rgb(255, 255, 255);
animation: move 5s linear infinite;
}
@keyframes move{
100%{
transform: rotate(360deg);
}
}
.water::before{
content: '';
position: absolute;
top: -360px;
left: -100px;
width: 400px;
height: 400px;
border-radius: 45%;
background-color: rgba(255, 255, 255,.5);
animation: move2 7s linear infinite;
}
@keyframes move2{
100%{
transform: rotate(360deg);
}
}
.shadow{
position: absolute;
bottom: -8px;
left: -3%;
width: 106%;
background-image: linear-gradient(0deg,rgb(9, 198, 245),rgb(44, 243, 120));
z-index: -1;
animation: bianse 12s linear forwards;
}
@keyframes bianse{
0%{
height: 50px;
filter: hue-rotate(0deg) blur(10px);
}
100%{
height: 80%;
filter: hue-rotate(360deg) blur(10px);
}
}
</style>
</head>
<body>
<div class="container">
<div class="water"></div>
<div class="shadow"></div>
</div>
</body>
</html>
復(fù)制代碼
總結(jié):
這個(gè)效果跟水波加載動(dòng)畫 html+css是異曲同工之妙的。效果不難實(shí)現(xiàn),但是這個(gè)‘偷天換日’的用法是值得學(xué)習(xí)的。
