按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css
https://codepen.io/comehope/pen/MqqqwGhtml
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端
請用 chrome, safari, edge 打開觀看。git
https://scrimba.com/p/pEgDAM/cJBwwHngithub
每日前端實戰系列的所有源代碼請從 github 下載:chrome
https://github.com/comehope/front-end-daily-challengesdom
定義 dom,容器中包含 9 個元素:佈局
<div class="container"> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> </div>
居中顯示:flex
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: black; }
定義容器尺寸:動畫
.container { width: 30em; height: 30em; font-size: 10px; }
用 grid 佈局把 9 個子元素排列成 3 * 3 的網格狀:
.container { display: grid; grid-template-columns: repeat(3, 1fr); }
設置容器內子元素的樣式,是經過僞元素來設置的:
.container span { position: relative; } .container span::before, .container span::after { content: ''; position: absolute; box-sizing: border-box; border-style: none solid solid none; border-width: 1em; border-color: gold; width: 100%; height: 100%; }
旋轉容器,讓它的尖端朝上:
.container { transform: rotate(-135deg); }
增長子元素尺寸由小到大變化的動畫:
.container span::before, .container span::after { animation: animate-scale 1.6s linear infinite; } @keyframes animate-scale { from { width: 1%; height: 1%; } to { width: 100%; height: 100%; } }
增長子元素邊框色變化的動畫:
.container span::before, .container span::after { animation: animate-border-color 1.6s linear infinite, animate-scale 1.6s linear infinite; } @keyframes animate-border-color { 0%, 25% { border-color: tomato; } 50%, 75% { border-color: gold; } 100% { border-color: black; } }
增長子元素邊框寬度變化的動畫:
.container span::before, .container span::after { animation: animate-border-width 1.6s linear infinite, animate-border-color 1.6s linear infinite, animate-scale 1.6s linear infinite; }
最後,讓 ::after
僞元素的動畫時間慢半拍:
.container span::after { animation-delay: -0.8s; } @keyframes animate-border-width { 0%, 100%{ border-width: 0.1em; } 25% { border-width: 1.5em; } }
大功告成!