为了账号安全,请及时绑定邮箱和手机立即绑定

前端每日实战: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);     } }

大功告成!

原文链接:https://segmentfault.com/a/1190000015978181

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消