按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/gKxyWohtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/cg48mtygithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesapp
定義 dom,容器中包含一個圓環和3個小球:dom
<div class="container"> <div class="ring"></div> <div class="spheres"> <span class="sphere"></span> <span class="sphere"></span> <span class="sphere"></span> </div> </div>
居中顯示:flex
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: darkslategray; }
改變盒模型:動畫
* { box-sizing: border-box; }
畫出圓環:
.container { position: relative; font-size: 20px; } .ring { position: relative; width: 10em; height: 10em; border: 1.5em solid paleturquoise; border-radius: 50%; }
在圓環的左上方畫出一個小球:
.sphere { position: absolute; top: -20%; left: -20%; } .sphere::after { content: ''; position: absolute; width: 3em; height: 3em; background-color: lightseagreen; border-radius: 50%; }
讓小球在圓環的左上方盤旋:
.sphere { width: 80%; height: 80%; animation: rotate 1.5s linear infinite; } @keyframes rotate { to { transform: rotate(360deg); } }
讓小球的圓環的上下穿梭:
.ring { z-index: 2; } .sphere { animation: rotate 1.5s linear infinite, overlapping 1.5s linear infinite; } @keyframes overlapping { to { z-index: 2; } }
經過設置動畫延時,製造 3 個小球同時盤旋的效果:
.sphere:nth-child(2) { animation-delay: -0.5s; } .sphere:nth-child(3) { animation-delay: -1s; }
最後,讓容器轉動起來,製造小球圍繞圓環盤旋的效果:
.container { animation: rotate 5s linear infinite; }
大功告成!