前端每日实战:82# 视频演示如何用纯 CSS 创做一个跳动的字母 i

图片描述

效果预览

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

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

可交互视频

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

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

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

源代码下载

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

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

代码解读

定义 dom,只有一个元素:flex

<div class="loader"></div>

居中显示:spa

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: black;
}

定义容器尺寸:code

.loader {
    width: 8em;
    height: 10em;
    font-size: 10px;
}

画出字母 i 的形状:

.loader {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.loader::before {
    content: '';
    width: 5em;
    height: 5em;
    background-color: orangered;
    border-radius: 50%;
}

.loader::after {
    content: '';
    width: 5em;
    height: 8em;
    background-color: orange;
    border-radius: 0.5em;
}

增长下部矩形的旋转效果:

.loader::after {
    animation: rect-rotating 1s ease-in-out infinite;
}

@keyframes rect-rotating {
    50% {
        transform: rotate(90deg);
    }

    100% {
        transform: rotate(180deg);
    }
}

增长上部小球的跳动效果:

.loader::before {
    animation: ball-jumping 1s ease-in-out infinite;
}

@keyframes ball-jumping {
    20%, 80% {
        transform: translateY(-2em);
    }

    50% {
        transform: translateY(calc((8em - 5em) / 2));
    }
}

大功告成!

相关文章
相关标签/搜索