僞類常常與僞元素混淆,僞元素的效果相似於經過添加一個實際的元素才能達到,而僞類的效果相似於經過添加一個實際的類來達到。實際上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
效果:(完整代碼見後文連接)
svg
實現原理:三邊設置邊框,箭頭指向的那個方向的border不用設置,位於箭頭兩邊的邊框顏色爲透明(transparent),對邊爲主體邊框顏色(較大的)/主體背景顏色(較小的),由於咱們要有邊框顏色的三角箭頭,當第一個箭頭(較大的)被第二個箭頭(較小的)經過準確覆蓋以後剩下沒被覆蓋的邊緣就是合成三角箭頭的邊框了,其顏色就是較大的那個三角箭頭的顏色,可調。而較小的那個三角箭頭的顏色要設置成主體顏色,進行負值定位偏移時要把主體邊框蓋住,從而與主體合在一塊兒了post
<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頁面騷氣起來》