前端每日实战:56# 视频演示如何用纯 CSS 描述程序员的生活

图片描述

效果预览

按下右侧的“点击预览”按钮能够在当前页面预览,点击连接能够全屏预览。css

https://codepen.io/comehope/pen/YvYVvYhtml

可交互视频

此视频是能够交互的,你能够随时暂停视频,编辑视频中的代码。前端

请用 chrome, safari, edge 打开观看。git

https://scrimba.com/p/pEgDAM/cN6L9SZgithub

源代码下载

每日前端实战系列的所有源代码请从 github 下载:chrome

https://github.com/comehope/front-end-daily-challengesdom

代码解读

定义 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>

居中显示:flex

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;
}

大功告成!

相关文章
相关标签/搜索