【動畫消消樂 】一個小清新型別的全域性網頁過渡動畫 075
阿新 • • 發佈:2021-08-04
前言
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="box"> <div></div> <div></div> <div></div> <div></div> </div> </section> </body> </html>
CSS
html, body { margin: 0; height: 100%; } body { display: flex; justify-content: center; align-items: center; background-color: #5e825a; 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; } .box { width: 100px; height: 100px; display: flex; flex-wrap: wrap; } .box>div:nth-child(1) { width: 50px; height: 50px; background-color: #c0f0b9; animation: load1 4s infinite; } .box>div:nth-child(2) { width: 50px; height: 50px; background-color: #f0d8ad; animation: load2 4s infinite; } .box>div:nth-child(3) { width: 50px; height: 50px; background-color: #f0addb; animation: load3 4s infinite; } .box>div:nth-child(4) { width: 50px; height: 50px; background-color: #c4d8f0; animation: load4 4s infinite; } @keyframes load1 { from { transform: translate(0); } 25% { transform: translate(100%); } 50% { transform: translate(100%, 100%); } 75% { transform: translate(0, 100%); } } @keyframes load2 { from { transform: translate(0); } 25% { transform: translate(0, 100%); } 50% { transform: translate(-100%, 100%); } 75% { transform: translate(-100%, 0); } } @keyframes load3 { from { transform: translate(0); } 25% { transform: translate(0, -100%); } 50% { transform: translate(100%, -100%); } 75% { transform: translate(100%); } } @keyframes load4 { from { transform: translate(0); } 25% { transform: translate(-100%, 0); } 50% { transform: translate(-100%, -100%); } 75% { transform: translate(0, -100%); } } @keyframes backColor { from { background-color: #5e825a; } 25% { background-color: #82466e; } 50% { background-color: #425e82; } 75% { background-color: #423827; } }
原理詳解
步驟1
使用一個div作為包含四個小方塊的大容器
其中每個小方塊也是用一個div表示
<div class="box">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
設定為
- 寬度、高度均為100px
- flex佈局
- 背景色:藍色
.box {
width: 100px;
height: 100px;
display: flex;
flex-wrap: wrap;// 換行
background-color: blue;
}
效果圖如下
步驟2
分別設定四個小方塊 均勻分佈
- 寬度、高度均為50px
- 顏色為四種(自定義)
.box>div:nth-child(1) {
width: 50px;
height: 50px;
background-color: #c0f0b9;
}
.box>div:nth-child(2) {
width: 50px;
height: 50px;
background-color: #f0d8ad;
}
.box>div:nth-child(3) {
width: 50px;
height: 50px;
background-color: #f0addb;
}
.box>div:nth-child(4) {
width: 50px;
height: 50px;
background-color: #c4d8f0;
}
效果圖如下
步驟3
為每個小方塊新增動畫
這裡以一個方塊為例
動畫簡化為關鍵四個步驟
- 右移
- 再下移
- 再左移
- 最後上移
右移說明:
下移說明:
左移說明:
上移說明:
主要藉助transform屬性進行方塊的移動
.box>div:nth-child(1) {
animation: load1 4s infinite;
}
@keyframes load1 {
from {
transform: translate(0);
}
25% {
transform: translate(100%);
}
50% {
transform: translate(100%, 100%);
}
75% {
transform: translate(0, 100%);
}
}
方塊的移動效果如下
注意:translate(x, y)是以最開始的位置作為參考點的
步驟4
其他方塊的動畫原理也是一樣的
不同的就是起始位置不同
編寫動畫效果的時候注意下需要移動方向的順序即可(一共就4個移動方向 順序可以組合)
.box>div:nth-child(1) {
animation: load1 4s infinite;
}
.box>div:nth-child(2) {
animation: load2 4s infinite;
}
.box>div:nth-child(3) {
animation: load3 4s infinite;
}
.box>div:nth-child(4) {
animation: load4 4s infinite;
}
@keyframes load1 {
from {
transform: translate(0);
}
25% {
transform: translate(100%);
}
50% {
transform: translate(100%, 100%);
}
75% {
transform: translate(0, 100%);
}
}
@keyframes load2 {
from {
transform: translate(0);
}
25% {
transform: translate(0, 100%);
}
50% {
transform: translate(-100%, 100%);
}
75% {
transform: translate(-100%, 0);
}
}
@keyframes load3 {
from {
transform: translate(0);
}
25% {
transform: translate(0, -100%);
}
50% {
transform: translate(100%, -100%);
}
75% {
transform: translate(100%);
}
}
@keyframes load4 {
from {
transform: translate(0);
}
25% {
transform: translate(-100%, 0);
}
50% {
transform: translate(-100%, -100%);
}
75% {
transform: translate(0, -100%);
}
}
效果圖如下
步驟5
註釋掉box的背景色
.box {
width: 100px;
height: 100px;
display: flex;
flex-wrap: wrap;
/* background-color: blue; */
}
步驟6
在全域性背景設定中新增動畫
使得全域性背景顏色隨著方塊的移動而隨著變色
body {
animation: backColor 4s infinite;
}
@keyframes backColor {
from {
background-color: #5e825a;
}
25% {
background-color: #82466e;
}
50% {
background-color: #425e82;
}
75% {
background-color: #423827;
}
}
得到最終效果圖
結語
文章僅作為學習筆記,記錄從0到1的一個過程。
希望對您有所幫助,如有錯誤歡迎小夥伴指正~
我是海轟ଘ(੭ˊᵕˋ)੭,如果您覺得寫得可以的話,請點個贊吧
寫作不易 「點贊」+「收藏」+「轉發」
謝謝支援❤️