栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

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

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

前端每日实战: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 个圆环:

                                                   

居中显示:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/242804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号