效果預覽
按下右側的“點擊預覽”按鈕可以在當前頁面預覽,點擊鏈接可以全屏預覽。
https://codepen.io/comehope/pen/YvYVvY
可交互視頻
此視頻是可以交互的,你可以隨時暫停視頻,編輯視頻中的代碼。
請用 chrome, safari, edge 打開觀看。
https://scrimba.com/p/pEgDAM/cN6L9SZ
源代碼下載
每日前端實戰系列的全部源代碼請從 github 下載:
https://github.com/comehope/front-end-daily-challenges
代碼解讀
定義 dom,容器中包含 6 個段落,每個段落 1 行代碼:
<div class="code"><p>function repeat() {</p><p> eat();</p><p> sleep();</p><p> code();</p><p> repeat();</p><p>}</p>
</div>
居中顯示:
body {margin: 0;height: 100vh;display: flex;align-items: center;justify-content: center;
}
代碼布局:
.code {background-color: silver;padding: 1em 0;font-size: 24px;font-family: monospace;border-radius: 0.5em;
}.code p {white-space: pre;padding: 0 1em;margin: 0.5em;
}
定義動畫:
.code p:not(:last-child) {animation: step 2s infinite;
}@keyframes step {0%, 25% {color: white;background-color: dodgerblue;}26%, 100% {color: black;background-color: transparent;}
}
設置動畫延時,描述單步執行的場景:
.code p:not(:last-child) {animation-delay: var(--d);
}.code p:nth-child(2) {--d: 0s;
}.code p:nth-child(3) {--d: 0.5s;
}.code p:nth-child(4) {--d: 1s;
}.code p:nth-child(1),
.code p:nth-child(5) {--d: 1.5s;
}
大功告成!