前端每日实战:80# 视频演示如何用纯 CSS 创作一个自行车车轮
效果预览
按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。
https://codepen.io/comehope/pen/XBXEPK
可交互视频
此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。
请用 chrome, safari, edge 打开观看。
https://scrimba.com/p/pEgDAM/cgkqnAz
源代码下载
每日前端实战系列的全部源代码请从 github 下载:
https://github.com/comehope/front-end-daily-challenges
代码解读
定义 dom,容器中包含 6 个元素:
<div class="wheel"> <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-image: linear-gradient(#555, #222); }
画出轮圈:
.wheel { width: 9em; height: 9em; font-size: 25px; border: 0.4em solid #777; border-radius: 50%; box-shadow: 0 0 0 0.5em #111; }
定义辐条的样式:
.wheel { display: flex; align-items: center; justify-content: center; } .wheel span { position: absolute; width: 8em; height: 1em; border: 0.1em solid; border-color: #ccc transparent; }
定义变量,画出多根幅条:
.wheel span { transform: rotate(calc((var(--n) - 1) * 30deg)); } .wheel span:nth-child(1) { --n: 1; } .wheel span:nth-child(2) { --n: 2; } .wheel span:nth-child(3) { --n: 3; } .wheel span:nth-child(4) { --n: 4; } .wheel span:nth-child(5) { --n: 5; } .wheel span:nth-child(6) { --n: 6; }
让车轮转动起来:
.wheel span { animation: run 4s linear infinite; } @keyframes run { to { transform: rotate(calc((var(--n) - 1) * 30deg + 360deg)); } }
用伪元素画出地面上的线条:
.wheel { position: relative; } .wheel::before { content: ''; position: absolute; width: 15em; height: 0.2em; top: 11em; background-image: linear-gradient( to right, silver 0, silver 4em, transparent 4em, transparent 5em, silver 5em, silver 10em, transparent 10em, transparent 12em, silver 12em, silver 14em, transparent 14em, transparent 15em ); }
最后,让地面上的线条动起来,形成车轮向前走的效果:
.wheel::before { background-position: 15em; animation: run2 6s linear infinite; } @keyframes run2 { to { background-position: -15em; } }
大功告成!
相关推荐
lanzhusiyu 2020-07-18
momode 2020-09-11
覆雪蓝枫 2020-06-09
opspider 2020-06-02
沈宫新 2020-04-30
zengni 2020-04-07
somboy 2020-01-07
葉無聞 2019-12-30
JenniferMuMu 2018-04-20
realitycss 2018-04-13
srdwxA 2018-03-16
hfszy0 2018-03-06
LadyZhong 2019-07-01
Phoebe的学习天地 2018-07-25