按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/eKzjqKhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/c8eyJUEgithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesdom
定義 dom,一個容器中包含 9 個子元素:flex
<div class="loader"> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> </div>
居中顯示:動畫
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: black; }
定義容器和子元素尺寸,是一個大正方形裏包含 9 個小正方形:spa
.loader { width: 10em; height: 10em; display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 0.5em; }
把圖案調整爲大菱形中包含 9 個小菱形:
.loader { transform: rotate(45deg); }
以豎向的小菱形爲單位,爲小菱形塊上色:
.loader span { background-color: var(--c); } .loader span:nth-child(7) { --c: tomato; } .loader span:nth-child(4), .loader span:nth-child(8) { --c: gold; } .loader span:nth-child(1), .loader span:nth-child(5), .loader span:nth-child(9) { --c: limegreen; } .loader span:nth-child(2), .loader span:nth-child(6) { --c: dodgerblue; } .loader span:nth-child(3) { --c: mediumpurple; }
定義動畫效果:
.loader span { animation: blinking 2s linear infinite; animation-delay: var(--d); transform: scale(0); } @keyframes blinking { 0%, 100% { transform: scale(0); } 40%, 80% { transform: scale(1); } }
最後,爲小菱形設置時延,加強動感:
.loader span:nth-child(7) { --d: 0s; } .loader span:nth-child(4), .loader span:nth-child(8) { --d: 0.2s; } .loader span:nth-child(1), .loader span:nth-child(5), .loader span:nth-child(9) { --d: 0.4s; } .loader span:nth-child(2), .loader span:nth-child(6) { --d: 0.6s; } .loader span:nth-child(3) { --d: 0.8s; }
大功告成!