伪类常常与伪元素混淆,伪元素的效果相似于经过添加一个实际的元素才能达到,而伪类的效果相似于经过添加一个实际的类来达到。实际上css3为了区分二者,已经明确规定了伪类用一个冒号来表示,而伪元素则用两个冒号来表示。伪类与伪元素的本质区别就是是否抽象创造了新元素。具体的伪类和伪元素相关知识本文就不深刻,下面介绍一下从青铜到王者10个css3伪类使用技巧和运用。直击案例代码css
众所周知,Animate/transition box-shadow 可使用box-shadow属性来实现盒子阴影效果,但repaint消耗较多,因而这里提出经过修改伪元素的透明度来实现盒子阴影前端
实现原理:vue
经过改变透明度,这样从一个非默认值更新它的值,就不须要承担任何重绘(参见:https://csstriggers.com/opacity)css3
(ps:貌似莫名的解锁了一个关于前端css优化,坏笑坏笑)git
这里设置一个空的伪元素设置阴影透明度为0隐藏,再经过鼠标悬停恢复它的透明度,下面是传统和伪类实现的代码对比github
<div class="before"> <h1>Before</h1> <p>Animate/transition box-shadow 可使用box-shadow属性来实现盒子阴影效果,但重绘消耗较多</p> </div> <hr /> <div class="after"> <h1>After</h1> <p>经过修改伪元素的透明度来实现一样的效果,没有重绘消耗</p> </div> 复制代码
.before { padding: 1em; background-color: #fff; -webkit-transition: 0.2s; transition: 0.2s; } .before:hover { box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3); } .after { position: relative; padding: 1em; background-color: #fff; } .after:before { content: ""; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: -1; box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3); opacity: 0; will-change: opacity; -webkit-transition: 0.2s; transition: 0.2s; } .after:hover:before { opacity: 1; } 复制代码
效果:(完整代码见后文连接)web
<ul class="breadcrumb"> <li><a href="#">Home</a> </li> <li><a href="#">Pictures</a> </li> <li><a href="#">Summer 15</a> </li> <li>Italy</li> </ul> 复制代码
ul.breadcrumb { padding: 8px 16px; list-style: none; background-color: #eee; } ul.breadcrumb li { display: inline; } ul.breadcrumb li+li:before { padding: 8px; color: black; content: "/\00a0"; } ul.breadcrumb li a { color: green; } 复制代码
效果:bash
效果:(完整代码见后文连接)
markdown
实现原理:三边设置边框,箭头指向的那个方向的border不用设置,位于箭头两边的边框颜色为透明(transparent),对边为主体边框颜色(较大的)/主体背景颜色(较小的),由于咱们要有边框颜色的三角箭头,当第一个箭头(较大的)被第二个箭头(较小的)经过准确覆盖以后剩下没被覆盖的边缘就是合成三角箭头的边框了,其颜色就是较大的那个三角箭头的颜色,可调。而较小的那个三角箭头的颜色要设置成主体颜色,进行负值定位偏移时要把主体边框盖住,从而与主体合在一块儿了svg
<div class='container'> <img alt='' src='http://placehold.it/400x200'> <div class='arrow-left'></div> </div> <div class='container new'> <div class='arrow-right'></div> <img alt='' src='http://placehold.it/400x200'> </div> 复制代码
.arrow-left:before { z-index: 9999; content: ""; display: block; width: 0; height: 0; border-top: 20px solid transparent; border-bottom: 20px solid transparent; border-right: 20px solid #E9E9E9; position: absolute; left: -20px; top: 80px; } 复制代码
效果:(完整代码见后文连接)
效果:(完整代码见后文连接)
原理:修改webkit-transform: skewY属性来修改倾斜度(旋转也是同样的道理)
.edge--bottom { position: relative; z-index: 1; } .edge--bottom:after { background: inherit; content: ''; display: block; height: 50%; left: 0; position: absolute; right: 0; z-index: -1; } .edge--bottom:after { bottom: 0; -webkit-transform: skewY(-1.5deg); -ms-transform: skewY(-1.5deg); transform: skewY(-1.5deg); -webkit-transform-origin: 100%; -ms-transform-origin: 100%; transform-origin: 100%; } 复制代码
效果:(完整代码见本文结尾连接)
<div class="row"> <a rel="nofollow" rel="noreferrer" href="#" class="btn tooltip top"> <span>TOOLTIP TOP</span> <span class="tooltip-content">Lorem ipsum dolor sit amet</span> </a> </div> 复制代码
.tooltip .tooltip-content::after { background: #05a8ff; content: ""; height: 10px; position: absolute; -webkit-transform: rotate(45deg); transform: rotate(45deg); width: 10px; } .tooltip.top .tooltip-content { bottom: calc(100% + 1.5em); left: 50%; -webkit-transform: translateX(-50%); transform: translateX(-50%); } .tooltip.top .tooltip-content::after { bottom: -5px; left: 50%; margin-left: -5px; } 复制代码
效果:(完整代码见本文结尾连接)
原理:Typing Animation with Pseudo-Elements 看起来是打字,实际上是使用伪元素覆盖在字符串上,而后逐渐减小伪元素覆盖宽度来实现的视觉效果
<div> <h1>Typing Animation</h1> <p class="tagline"> <span class="tagline-skill"><span class="tagline-skill_inner">webdesign</span></span> </p> </div> 复制代码
.tagline-skill_inner:after { content: ""; position: absolute; top: -1px; right: 0; bottom: -2px; left: 0; border-left: 1px solid #fff; background-color: #2a2a28; -webkit-animation: animatetoright 1s steps(10) infinite alternate; animation: animatetoright 1s steps(10) infinite alternate; } 复制代码
效果:(完整代码见本文结尾连接)
h1 { position: relative; margin: 0; font-weight: bold; letter-spacing: -0.05rem; line-height: 1; text-transform: uppercase; z-index: 10; } h1:before { content: "2018/08"; font-family: monospace; font-size: 10rem; position: absolute; top: 2rem; left: -2rem; z-index: 0; line-height: 1; color: rgba(50, 25, 0, 0.1); } 复制代码
效果:(完整代码见本文结尾连接)
a { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-flow: row nowrap; -ms-flex-flow: row nowrap; flex-flow: row nowrap; -webkit-box-align: baseline; -webkit-align-items: baseline; -ms-flex-align: baseline; align-items: baseline; text-decoration: none; -webkit-transition: color .2s ease-in-out; transition: color .2s ease-in-out; } a::before { height: .1em; -webkit-box-flex: 1; -webkit-flex: 1 1 auto; -ms-flex: 1 1 auto; flex: 1 1 auto; -webkit-box-ordinal-group: 2; -webkit-order: 1; -ms-flex-order: 1; order: 1; background: left bottom/contain repeat-x url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA3IDIiPjxjaXJjbGUgZmlsbD0iI2ZmZiIgY3g9IjMuNSIgY3k9IjEiIHI9IjEiLz48L3N2Zz4=); content: ''; } a::after { -webkit-box-ordinal-group: 3; -webkit-order: 2; -ms-flex-order: 2; order: 2; content: "p." attr(data-page); } 复制代码
效果:(完整代码见本文结尾连接)
一、IE8不支持CSS3中不少特性,好比伪元素nth-child,可使用+号(表明相邻元素)来实现相同功能
二、Google的IE9.js是解决IE5.5到IE8 CSS3特性兼容性问题的JS库
CSS的世界很美好,每一个知识点均可以值得深刻研究和实践,对于伪类、伪元素也有不少土味特效能够写出来,好比说图片遮罩、图片背景模糊,更多高级的鼠标通过事件特效等等,上边的10个案例是我我的工做上总结和参考踏得网上资源整理,但愿对你们有所帮助
最后附上github示例源码,可下载体验。
接下来的下篇,我将写一些在vue页面上交互上的的运用技巧,同样都是干货,该篇文章传送门《当大多数人对Vue理解到炉火纯青的时候,是否是该思考一下怎么让vue页面骚气起来》