前端每日实战:103# 视频演示如何用纯 CSS 创作一只监视眼

《前端每日实战:103# 视频演示如何用纯 CSS 创作一只监视眼》

效果预览

按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

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

可交互视频

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

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

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

源代码下载

每日前端实战系列的全部源代码请从 github 下载:

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

代码解读

定义 dom,容器中包含 10 个元素,每个元素代表 1 个圆环:

<div class="circles">
    <span></span>
    <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: lightgoldenrodyellow;
}

定义容器尺寸和其中子元素的布局方式:

.circles {
    width: 10em;
    height: 10em;
    font-size: 30px;
    border: 1px dashed black;
    display: flex;
    justify-content: center;
}

为每个圆环定义下标变量:

.circles span:nth-child(1) {
    --n: 1;
}

.circles span:nth-child(2) {
    --n: 2;
}

.circles span:nth-child(3) {
    --n: 3;
}

.circles span:nth-child(4) {
    --n: 4;
}

.circles span:nth-child(5) {
    --n: 5;
}

.circles span:nth-child(6) {
    --n: 6;
}

.circles span:nth-child(7) {
    --n: 7;
}

.circles span:nth-child(8) {
    --n: 8;
}

.circles span:nth-child(9) {
    --n: 9;
}

.circles span:nth-child(10) {
    --n: 10;
}

设置每个的圆环的尺寸,颜色黑白间隔:

.circles {
    position: relative;
}

.circles span {
    position: absolute;
    --diameter: calc(10em - (var(--n) - 1) * 1em);
    width: var(--diameter);
    height: var(--diameter);
    border-radius: 50%;
}

.circles span:nth-child(odd) {
    background-color: darkred;
}

.circles span:nth-child(even) {
    background-color: gold;
}

把尺寸最小的 3 个圆环向下移动,形成凹陷的效果:

.circles span:nth-child(n+8) {
    top: calc((var(--n) - 7) * 1em);
}

让容器旋转起来,就好像一只监视的眼睛转来转去:

.circles {
    animation: rotating 5s linear infinite;
}

@keyframes rotating {
    to {
        transform: rotate(1turn);
    }
}

大功告成!

    原文作者:comehope
    原文地址: https://segmentfault.com/a/1190000015978181
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞