今天咱们来复盘一下前端中css伪元素的知识以及如何用css伪元素来减轻javascript的压力,作出一些脑洞大开的图形。javascript
伪元素是一个附加至选择器末的关键词,容许你对被选择元素的特定部分修改样式。伪元素主要有:css
我我的以为伪元素能够解释为元素的修饰,能够为元素带来额外的附加样式,属于额外的文档结构。html
用来表示没法在CSS中轻松或者可靠检测到的某个元素的状态或属性,好比a标签的hover表示鼠标通过的样式,visited表示访问过的连接的样式,更多的用来描述元素状态变化时的样式,伪类主要有:前端
咱们利用css伪类和伪元素能够实现不少强大的视觉效果,这里我主要介绍伪元素,若是对伪类或其余css特性感兴趣,能够看看我以前的css文章,写的很全面:vue
用css3实现惊艳面试官的背景即背景动画(高级附源码)java
css3实战汇总(附源码)node
先看看咱们用纯css实现的图标库:react
接下来我会介绍实现如上图标的方式和方法,并给出部分源码,方便你们学习。css3
咱们实现如上css图标是基于伪元素的,能够利用伪元素的::before和::after和content属性来为元素添加额外视觉效果,咱们在上文中也介绍了伪元素的概念和类型,接下来让咱们来实现它吧~es6
// less
.arrow {
position: relative;
display: inline-block;
line-height: 0;
background-color: #ccc;
&.arrow-hor {
width: 16px;
height: 1px;
}
&.arrow-hor.right::before {
content: '';
position: absolute;
top: -4px;
right: -8px;
border: 4px solid transparent;
border-left: 4px solid #ccc;
}
}
// html
<span class="arrow arrow-hor right"></span>
复制代码
这样就实现了一个指向右的箭头,咱们用相似的方法也能够实现左箭头,上下箭头,实现双向箭头只须要加一个::after伪类并作适当定位就行了。
// less
.search {
position: relative;
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
border: 1px solid #ccc;
text-align: center;
transform: rotate(-45deg);
&::after {
content: '';
display: inline-block;
width: 1px;
height: 4px;
background-color: #ccc;
}
}
// html
<span class="search"></span>
复制代码
// less
.dot-pan {
position: relative;
display: inline-flex;
width: 60px;
height: 60px;
line-height: 0;
align-items: center;
border-radius: 50%;
background-color: #06c;
transform: rotate(-90deg);
&::before {
content: '';
display: inline-block;
width: 28px;
height: 40px;
margin-left: -3px;
border-radius: 50% 50%;
flex-shrink: 0;
background-color: #fff;
}
&::after {
content: '';
display: inline-block;
width: 20px;
height: 20px;
flex-shrink: 0;
border-radius: 50%;
background-color: #fff;
}
}
// html
<span class="search"></span>
复制代码
// less
.locate-icon {
position: relative;
display: inline-block;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #06c;
&::before {
content: '';
position: absolute;
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
}
&::after {
content: '';
margin-top: 45px;
display: inline-block;
border: 15px solid transparent;
border-top-color: #06c;
}
}
// html
<span class="locate-icon mr-20"></span>
复制代码
// less
.wechat-icon {
display: inline-block;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgb(68, 170, 59);
&::before {
content: '';
margin-top: 14px;
position: absolute;
width: 4px;
height: 4px;
border-radius: 50%;
background-color: #fff;
box-shadow: 16px 0 0 #fff;
}
&::after {
content: '';
margin-top: 42px;
display: inline-block;
border-width: 6px 10px 6px 10px;
border-style: solid;
border-color: transparent;
border-top-color: rgb(68, 170, 59);
transform: rotate(-147deg);
}
}
// html
<span class="wechat-icon mr-20"></span>
复制代码
// less
.yes-icon {
display: inline-flex;
justify-content: center;
align-items: center;
width: 48px;
height: 48px;
background-color: green;
border-radius: 50%;
&::after {
content: '✓';
color: #fff;
font-size: 30px;
font-weight: bold;
}
}
// html
<span class="yes-icon mr-20"></span>
复制代码
.view-icon {
display: inline-flex;
justify-content: center;
align-items: center;
width: 58px;
height: 36px;
background-color: #06c;
border-radius: 50%;
&::after {
content: '';
display: inline-block;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: #fff;
}
}
复制代码
.gps-icon {
position: relative;
display: inline-flex;
justify-content: center;
align-items: center;
border-width: 30px 15px 30px 15px;
border-color: transparent;
border-style: solid;
border-bottom-color: #06c;
transform: translate(10px, -16px) rotate(32deg);
&::after {
position: absolute;
bottom: -48px;
content: '';
display: inline-block;
border-width: 10px 38px 30px 38px;
border-color: transparent;
border-style: solid;
border-bottom-color: #fff;
}
}
复制代码
.logo-x {
position: relative;
display: inline-flex;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgb(212, 73, 17);
&::after {
position: absolute;
content: '';
left: 10px;
top: 12px;
display: inline-block;
width: 20px;
height: 30px;
border-radius: 50%;
background-color: #fff;
transform: rotate(135deg);
}
&::before {
position: absolute;
content: '';
right: 10px;
top: 12px;
display: inline-block;
width: 20px;
height: 30px;
border-radius: 50%;
background-color: #fff;
transform: rotate(-135deg);
}
}
复制代码
.logo-ps {
position: relative;
display: inline-flex;
justify-content: center;
align-items: center;
width: 50px;
height: 50px;
border-radius: 8px;
background-color: rgb(15, 102, 184);
&::before {
position: absolute;
content: '';
display: inline-block;
width: 50px;
height: 40px;
background-color: rgba(255, 255, 255, .1);
}
&::after {
position: absolute;
content: 'PS';
font-size: 24px;
display: inline-block;
color: #fff;
}
}
复制代码
.logo-pp {
display: inline-block;
width: 150px;
height: 50px;
border-radius: 8px;
background-color: rgb(68, 170, 59);
&::before {
content: '等你下课哦!';
margin-top: 14px;
margin-left: -33px;
position: absolute;
color: #fff;
}
&::after {
content: '';
margin-top: 42px;
display: inline-block;
border-width: 6px 10px 6px 10px;
border-style: solid;
border-color: transparent;
border-top-color: rgb(68, 170, 59);
transform: rotate(-147deg) translate(-12px, 6px);
}
}
复制代码
因为篇幅缘由,其余的图标就不一一实现了,原理都相似,笔者以前曾利用这个方案作过一套100个图标的库,成功应用于各个项目中,因为体积小不会形成额外请求,因此更加实用,但更复杂的图形就为了方便仍是建议用字体图标或者svg,base64等。
若是想了解更多css,javascript,nodeJS等前端知识和实战,欢迎在公众号《趣谈前端》加入咱们一块儿学习讨论,共同探索前端的边界。