按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/yxVYRLhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/cmVLRc9github
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesdom
定義 dom,容器中包含 3 個子元素,分別表示機冀、螺旋槳和輪子,機冀有 4 片葉片,輪子左右各一隻:佈局
<div class="plane"> <div class="wings"></div> <div class="fans"> <span></span> <span></span> <span></span> <span></span> </div> <div class="wheels"> <span class="left"></span> <span class="right"></span> </div> </div>
定義容器尺寸:flex
.plane { width: 28em; height: 13em; font-size: 10px; }
定義子元素總體佈局和共有屬性:動畫
.plane { display: flex; justify-content: center; position: relative; } .plane > * { position: absolute; } .plane > *::before, .plane > *::after { content: ''; position: absolute; }
定義基本色:
.plane { color: black; }
畫出雙冀:
.wings { width: inherit; display: flex; justify-content: center; } .wings::before { width: inherit; height: 0.5em; background-color: currentColor; } .wings::after { top: 4em; width: 90%; height: 0.4em; background-color: currentColor; }
畫出螺旋槳的中心:
.fans { width: 11em; height: 11em; outline: 1px dashed; background: radial-gradient( black 2.5em, transparent 2.5em ); }
定義葉片的形狀爲半圓形:
.fans span { width: inherit; height: inherit; } .fans span::before { width: inherit; height: 50%; background-color: rgba(255, 255, 255, 0.4); border-radius: 50% 50% 0 0 / 100% 100% 0 0; }
分別旋轉葉片的角度,使 4 個頁片均勻分佈在一個圓內:
.fans span::before { transform-origin: bottom; transform: rotate(calc((var(--n) - 1) * 90deg)); } .fans span:nth-child(1) { --n: 1; } .fans span:nth-child(2) { --n: 2; } .fans span:nth-child(3) { --n: 3; } .fans span:nth-child(4) { --n: 4; }
畫出 2 個輪子:
.wheels { width: 16em; height: 2em; outline: 1px dashed; bottom: 0; display: flex; justify-content: space-between; } .wheels span { position: static; width: 1em; height: inherit; background-color: currentColor; border-radius: 0.5em; }
畫出輪子的 2 個支架:
.wheels span { display: flex; justify-content: center; } .wheels span::before { width: 0.2em; height: 8em; background-color: currentColor; transform-origin: bottom; bottom: 1em; z-index: -1; } .wheels .left::before { transform: rotate(45deg); } .wheels .right::before { transform: rotate(-45deg); }
接下來製做動畫效果。
增長螺旋槳旋轉的動畫效果:
.fans span { animation: fans-rotating 0.8s linear infinite; animation-delay: calc(var(--n) * 0.1s); } @keyframes fans-rotating { to { transform: rotate(-1turn); } }
增長飛機飛行的動畫效果:
.plane { animation: fly 5s infinite; } @keyframes fly { 10%, 50%, 100% { top: 0; } 25% { top: 1em; } 75% { top: -1em; } }
再增長飛機旋轉的動畫效果:
.plane { animation: plane-rotating 10s infinite, fly 5s infinite; } @keyframes plane-rotating { 10%, 30%, 50% { transform: rotate(0deg); } 20% { transform: rotate(-4deg); } 80% { transform: rotate(8deg); } 100% { transform: rotate(-1turn); } }
大功告成!