按下右侧的“点击预览”按钮能够在当前页面预览,点击连接能够全屏预览。css
https://codepen.io/comehope/pen/xzYVzOhtml
此视频是能够交互的,你能够随时暂停视频,编辑视频中的代码。前端
请用 chrome, safari, edge 打开观看。git
https://scrimba.com/p/pEgDAM/cRkRLsmgithub
每日前端实战系列的所有源代码请从 github 下载:chrome
https://github.com/comehope/front-end-daily-challengesdom
定义 dom,容器中包含 2 个元素:flex
<div class="eyes"> <span class="left"></span> <span class="right"></span> </div>
居中显示:动画
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: black; }
定义容器尺寸:spa
.eyes { width: 40em; height: 10em; font-size: 10px; }
画出眼睛的轮廓:
.eyes { position: relative; } .eyes > * { box-sizing: border-box; position: absolute; width: 15em; height: 10em; border: solid white; } .eyes .left { left: 0; } .eyes .right { right: 0; }
画出眼球:
.eyes > * { border-width: 0 5em; } .eyes .left { border-radius: 50% 0; } .eyes .right { border-radius: 0 50%; }
使双眼向内聚拢:
.eyes .left { transform: rotate(25deg); } .eyes .right { transform: rotate(-25deg); }
定义眨眼的动画:
@keyframes blink { 40%, 60% { border-width: 0 5em; } 50% { border-width: 0 7.5em; } }
最后,把动画效果应用到两只眼睛上:
.eyes > * { animation: blink 2s linear infinite; }
大功告成!