按下右側的「點擊預覽」按鈕在當前頁面預覽,點擊連接全屏預覽。css
https://codepen.io/zhang-ou/pen/vjLQMMhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。git
請用 chrome, safari, edge 打開觀看。github
https://scrimba.com/c/cJMkwH9chrome
請從 github 下載。dom
定義 dom,一個包含 3 個 span 的容器:動畫
<div class="loader"> <span></span> <span></span> <span></span> </div>
居中顯示:spa
html, body { height: 100%; display: flex; align-items: center; justify-content: center; background-color: black; }
設置容器的尺寸:code
.loader { width: 150px; height: 150px; position: relative; }
設置矩形的邊框樣式:
.loader span { position: absolute; box-sizing: border-box; border: 10px solid dimgray; border-radius: 2px; }
設置 3 個矩形的尺寸:
.loader span:nth-child(1) { width: 100%; height: 100%; } .loader span:nth-child(2) { width: 70%; height: 70%; margin: 15%; } .loader span:nth-child(3) { width: 40%; height: 40%; margin: 30%; }
用僞元素繪製左上和右下的裝飾條:
.loader span::before, .loader span::after { content: ''; position: absolute; width: 10px; height: 50%; background-color: gold; } .loader span::before { top: -10px; left: -10px; } .loader span::after { bottom: -10px; right: -10px; }
定義動畫效果:
@keyframes rotating { from { transform: rotateY(0deg); } to { transform: rotateY(360deg); } }
把動畫應用到 3 個矩形上:
.loader span { animation: rotating linear infinite; } .loader span:nth-child(1) { animation-duration: 4s; } .loader span:nth-child(2) { animation-duration: 2s; } .loader span:nth-child(3) { animation-duration: 1s; }
最後,設置一下 3 個矩形的堆疊順序:
.loader span:nth-child(1) { z-index: 3; } .loader span:nth-child(2) { z-index: 2; } .loader span:nth-child(3) { z-index: 1; }
大功告成!