前端每日实战:120# 视频演示如何用纯 CSS 创作锡纸撕开的文字效果
效果预览
按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。
https://codepen.io/comehope/pen/WgxbaZ
可交互视频
此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。
请用 chrome, safari, edge 打开观看。
https://scrimba.com/p/pEgDAM/c86pbcE
源代码下载
每日前端实战系列的全部源代码请从 github 下载:
https://github.com/comehope/front-end-daily-challenges
代码解读
定义 dom,容器中包含若干子元素,每个子元素中包含一个字母:
<div class="text"> <span>A</span> <span>W</span> <span>E</span> <span>S</span> <span>O</span> <span>M</span> <span>E</span> </div>
定义容器尺寸:
body { margin: 0; height: 100vh; } .text { width: 100%; height: 100%; }
设置子元素的布局方式:
.text { display: flex; justify-content: space-between; } .text span { width: 100%; }
定义文本样式:
.text span { color: darkslategray; background-color: rgb(127, 140, 141); font-family: serif; font-size: 12vmin; text-shadow: 1px 1px 1px white; display: flex; align-items: center; justify-content: center; }
设置文本的背景的渐变色,奇数位的文字和偶数位的文字的渐变方向是相反的:
.text span:nth-child(odd) { background: linear-gradient( to bottom, rgba(127, 140, 141, 0.2) 0%, rgba(127, 140, 141, 0) 33%, rgba(127, 140, 141, 0.7) 66%, rgba(127, 140, 141, 0.2) 100% ); } .text span:nth-child(even) { background: linear-gradient( to top, rgba(127, 140, 141, 0.2) 0%, rgba(127, 140, 141, 0) 33%, rgba(127, 140, 141, 0.7) 66%, rgba(127, 140, 141, 0.2) 100% ); }
增加文字之间的分隔线,第1个文字之前不用加分隔线:
.text span { position: relative; } .text span:not(:first-child)::before { content: ''; position: absolute; width: 10px; height: 90%; background-color: black; left: -5px; border-left: 1px solid white; border-radius: 50%; }
让分隔线上下错位:
.text span:not(:first-child):nth-child(odd)::before { top: 2%; } .text span:not(:first-child):nth-child(even)::before { bottom: 2%; }
大功告成!
相关推荐
jiedinghui 2020-10-25
Ladyseven 2020-10-22
zuncle 2020-09-28
xiaohuli 2020-09-02
葉無聞 2020-09-01
nicepainkiller 2020-08-20
AlisaClass 2020-08-09
myloveqiqi 2020-08-09
buttonChan 2020-08-02
drdrsky 2020-07-29
Ladyseven 2020-07-25
nicepainkiller 2020-07-24
AlisaClass 2020-07-19
hellowzm 2020-07-19
background-color: blue;background-color: yellow;<input type="button" value="变蓝" @click="changeColorT
昔人已老 2020-07-19
骆驼的自白 2020-07-18
lanzhusiyu 2020-07-19
hellowzm 2020-07-19
CSSEIKOCS 2020-07-18