前端每日实战:58# 视频演示如何用纯 CSS 创作一只卡通鹦鹉
效果预览
按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。
https://codepen.io/comehope/pen/vrRmWy
可交互视频
此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。
请用 chrome, safari, edge 打开观看。
https://scrimba.com/p/pEgDAM/c7adzTW
源代码下载
每日前端实战系列的全部源代码请从 github 下载:
https://github.com/comehope/front-end-daily-challenges
代码解读
定义 dom,容器中包含 3 个子元素:
<div class="parrot"> <div class="outer"></div> <div class="middle"></div> <div class="inner"></div> </div>
居中显示:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: darkslategray; }
定义容器尺寸:
.parrot { width: 10em; height: 10em; font-size: 30px; }
画出鹦鹉头部的羽毛:
.parrot { position: relative; display: flex; align-items: center; justify-content: center; } .parrot .outer { position: absolute; width: 100%; height: 100%; border: 1em solid; border-color: transparent transparent orangered tomato; border-radius: 50%; }
画出鹦鹉的头部和喙的上半部分:
.parrot .middle { position: absolute; width: 80%; height: 80%; border: 4em solid; border-color: gold transparent gainsboro white; border-radius: 50%; }
画出鹦鹉的喙的下半部分:
.parrot .inner { position: absolute; width: 40%; height: 40%; border: 2em solid; border-color: transparent orange transparent transparent; border-radius: 50%; }
用伪元素画出鹦鹉的眼睛:
.parrot .inner::before { content: ''; position: absolute; width: 1em; height: 1em; background-color: black; border-radius: 50%; left: -2em; top: -0.5em; }
把画面转正:
.parrot > * { transform: rotate(45deg); }
设置鼠标悬停效果,悬停时鹦鹉的头转向另一侧:
.parrot > * { transition: 0.5s; } .parrot:hover .outer { transform: rotate(225deg); border-color: transparent transparent tomato orangered; } .parrot:hover .middle { transform: rotate(calc(225deg - 360deg)); border-color: transparent gold white gainsboro; } .parrot:hover .inner { transform: rotate(135deg); }
最后,修改悬停时鹦鹉的配色:
.parrot:hover .outer { border-color: transparent transparent lightseagreen darkcyan; } .parrot:hover .middle { border-color: transparent gold white gainsboro; } .parrot:hover .inner { border-color: transparent orange transparent transparent; }
大功告成!
相关推荐
zhanghao 2019-12-20
impress 2019-11-05
云端漂移 2020-07-06
懵懂听风雨 2018-09-27
jianqi 2019-06-28
momode 2020-09-11
MaureenChen 2020-02-17
bertzhang 2020-01-31
impress 2020-01-11
MaureenChen 2020-01-10
impress 2019-12-31
zengni 2019-12-18
zhanghao 2019-12-12
didianmanong 2019-11-17
MaureenChen 2019-11-03
echoes 2019-11-03
zhangruiweb 2019-11-01
绿豆饼 2019-10-20