css3的focus-within選擇器的使用

偽元素和偽類
說(shuō)到這個(gè),我們先回顧一下,偽類選擇器和偽元素選擇器,老版的瀏覽器沒(méi)有嚴(yán)格區(qū)分下面 2 種寫法。
a:after{} a::after{}
在新的標(biāo)準(zhǔn)中,單冒號(hào)(:)用于 CSS3 偽類,雙冒號(hào)(::)用于 CSS3 偽元素,我們平時(shí)開(kāi)發(fā)時(shí)可以注意一下,當(dāng)然大多數(shù)瀏覽器兩種寫法都能識(shí)別。
常見(jiàn)偽元素和偽類
偽類
:link, :visited, :hover, :active, :focus, :first-child, :last-child, :nth-child, :nth-last-child, :not()
偽類一般用于一個(gè)元素的某個(gè)狀態(tài),比如說(shuō)鼠標(biāo)懸浮,按鈕點(diǎn)擊,鏈接已經(jīng)訪問(wèn),輸入框聚焦等,還用于選擇某個(gè)特殊元素,比如說(shuō)多個(gè)元素中的第一個(gè),最后一個(gè),偶數(shù),奇數(shù)等。其作用是對(duì)某個(gè)符合以上條件的元素添加一些樣式。
a:hover{ text-decoration: underline; } a:active { color: blue; } a:link { color: red; } a:visited { color: green; }
上面的例子展示了一個(gè)a
標(biāo)簽在不同狀態(tài)下的不同樣式,在未點(diǎn)擊鏈接之前,a
標(biāo)簽呈現(xiàn)紅色字體(link),在鼠標(biāo)移到a
標(biāo)簽上是,a
標(biāo)簽出現(xiàn)下劃線(hover),在鼠標(biāo)按下的時(shí)候,a
標(biāo)簽變?yōu)樗{(lán)色(active),點(diǎn)擊完了之后,a
標(biāo)簽變?yōu)榫G色(visited)。可以看到,偽類的作用是為了給不同狀態(tài)的標(biāo)簽添加樣式。
偽元素
::first-letter, ::first-line, ::before, ::after
在內(nèi)容模塊中提到,偽元素如果沒(méi)有設(shè)置“content”屬性,偽元素是無(wú)用的。使用偽元素插入的內(nèi)容在頁(yè)面的源碼里是不可見(jiàn)的,只能在 css 里可見(jiàn)。插入的元素在默認(rèn)情況下是內(nèi)聯(lián)元素(或者,在 html5 中,在文本語(yǔ)義的類別里)。因此,為了給插入的元素賦予高度,填充,邊距等等,你通常必須顯式地定義它是一個(gè)塊級(jí)元素。還要注意的是典型的 CSS 繼承規(guī)則適用于插入的元素。例如,你有字體系列黑體,宋體,無(wú)襯線字體應(yīng)用到 body 元素里,然后偽元素會(huì)像其他元素一樣繼承這些字體系列。偽元素不會(huì)自然繼承自父元素(如 padding margins)的樣式。你的直覺(jué)是 :before 和 :after 偽元素可能是插入的內(nèi)容會(huì)被注入到目標(biāo)元素的前或后注入。其實(shí)不是這樣的,注入的內(nèi)容將是有關(guān)聯(lián)的目標(biāo)元素的子元素,但它會(huì)被置于這個(gè)元素的任何內(nèi)容的“前”或“后”。
<head> <style type="text/css"> p.box::before { content: "#"; border: solid 1px black; padding: 2px; margin: 0 10px 0 0; } p.box::after { content: "#"; border: solid 1px black; padding: 2px; margin: 0 10px 0 0; } </style> </head> <body> <p class="box">Other content.</p> </body>
運(yùn)行效果:
可以看到,我們html
部分只寫了一個(gè)元素,但是我們利用偽元素渲染出來(lái) 3 個(gè)部分,前中后,這里我們可以認(rèn)為,偽元素一般用來(lái)輔助html
的元素。但在內(nèi)容頁(yè)面的源碼又看不到,利用偽元素可以實(shí)現(xiàn)很多神奇的功能,這里不做具體講解,后面再出具體教程。
神奇的偽類:focus-within
言歸正傳,回到我們的主角focus-within
,我們知道,偽類focus
是指一個(gè)元素獲得焦點(diǎn)時(shí),為其添加樣式。focus-within
的范圍更廣,它表示一個(gè)元素獲得焦點(diǎn),或該元素的后代元素獲得焦點(diǎn)。劃重點(diǎn),它或它的后代獲得焦點(diǎn)。這也就意味著,它或它的后代獲得焦點(diǎn),都可以觸發(fā):focus-within
。
這個(gè)屬性有點(diǎn)類似Javascript
的事件冒泡,從可獲焦元素開(kāi)始一直冒泡到根元素html
,都可以接收觸發(fā):focus-within
事件,類似下面這個(gè)簡(jiǎn)單的例子這樣:
<html> <div class="box g-father"> <div class="box g-children"> <div class="box button" tabindex="1">button</div> </div> </div> <div class="g-body">HTML</div> <style> div { box-sizing: border-box; } .button,.g-children { width: 100%; height: 100%; padding: 20px; border: 1px solid; } .g-father { width: 200px; height: 200px; padding: 20px; border: 1px solid; } .g-body { margin-top: 20px; width: 200px; border: 1px solid; } .g-body:focus-within { background-color: #5daf34; } .g-father:focus-within { background-color: #3a8ee6; } .g-children:focus-within{ background-color: #2c3e50; } .button:focus-within { background-color: #606266; color: red; } </style> </html>
運(yùn)行結(jié)果:
可以看到,在button
獲得焦點(diǎn)時(shí),因?yàn)槊芭莸脑颍母讣?jí)元素全部應(yīng)用了:focus-within
的樣式。這里值得注意的是,正常的div
是不能獲得焦點(diǎn)的,設(shè)置 tabindex 屬性才能獲取焦點(diǎn),同時(shí)按鍵盤 Tab 鍵也可讓其獲取焦點(diǎn),其中 tabindex 的值越小在 tab 鍵切換的時(shí)候就會(huì)首先聚焦。根據(jù):focus-within
的特性,我們?cè)诓焕?js 的情況下,實(shí)現(xiàn)很多實(shí)用性的功能。
感應(yīng)用戶聚焦區(qū)域
利用focus-within
可以增加用戶的感知區(qū)域,讓用戶獲得更好的視覺(jué)反饋。
<html> <div class="g-container"> <input type="text" placeholder="user name" class="g_input" > <input type="text" placeholder="code" class="g_input" > </div> <style> .g-container { margin-top: 10vh; } .g-container { padding: 10px; width: 30vw; border: 1px solid #eee; transition: all .3s; text-align: center; } .g-container:focus-within { transform: translateY(-4px); box-shadow: 0 0 10px #ddd; border-color: hsl(199, 98%, 48%); } .g_input { border: none; width: 20vw; padding: 15px; font-size: 18px; box-sizing: border-box; border: 1px solid #ddd; overflow: hidden; transition: 0.3s; box-shadow: 0 0 0px #ddd; &:focus { box-shadow: 0 0 10px #ddd; border-color: hsl(199, 98%, 48%); } } </style> </html>
可以看到在沒(méi)有任何javascript
邏輯控制情況下,用focus-within
就實(shí)現(xiàn)了上面的效果。
實(shí)現(xiàn)離屏導(dǎo)航
我們先看一下效果:
可以看到是一個(gè)很棒的導(dǎo)航效果,而且真?zhèn)€實(shí)現(xiàn)沒(méi)有使用javascript
控制,這無(wú)疑在性能和體驗(yàn)上都有不少提升。具體源碼可以看下面的地址:地址
實(shí)現(xiàn) B 站,掘金等網(wǎng)站登錄動(dòng)效切換
我們平時(shí)可能注意到了,B 站和掘金在用戶輸入密碼的時(shí)候,上面的圖片是捂著眼睛的,這里我們也可以用focus-within
來(lái)實(shí)現(xiàn)。
<html> <div class="g-wrap"></div> <div class="g-container"> <h2>登錄</h2> <div class="g-username"> <input maxlength="64" placeholder="請(qǐng)輸入手機(jī)號(hào)或郵箱" class="input"> <img src="https://b-gold-cdn.xitu.io/v3/static/img/greeting.1415c1c.png" class="g-username"> </div> <div class="g-password"> <input type="password" maxlength="64" placeholder="請(qǐng)輸入密碼" class="input"> <img src="https://b-gold-cdn.xitu.io/v3/static/img/blindfold.58ce423.png" class="g-password"> </div> <img src="https://b-gold-cdn.xitu.io/v3/static/img/normal.0447fe9.png" class="g-normal"> </div> <style> .g-wrap { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background: rgba(0, 0, 0, 0.3); } .g-container { position: relative; width: 318px; margin: 100px auto; height: 370px; padding: 20px; box-sizing: border-box; background: #fff; z-index: 10; } .g-container h2 { font-size: 20px; font-weight: bold; margin-bottom: 30px; } .g-container input { outline: none; padding: 10px; width: 100%; border: 1px solid #e9e9e9; border-radius: 2px; outline: none; box-sizing: border-box; font-size: 16px; } img { position: absolute; top: -20%; left: 50%; width: 120px; height: 95px; transform: translate(-50%, 0); } .g-username { margin-bottom: 10px; } .g-username img { display: none; width: 120px; height: 113px; } .g-username:focus-within ~ img { display: none; } .g-username:focus-within input { border-color: #007fff; } .g-username:focus-within img { display: block; } .g-password { margin-bottom: 10px; } .g-password img { display: none; width: 103px; height: 84px; top: -15%; } .g-password:focus-within ~ img { display: none; } .g-password:focus-within input { border-color: #007fff; } .g-password:focus-within img { display: block; } </style> </html>
可以看到,在不適用js
的情況下,也能實(shí)現(xiàn)動(dòng)態(tài)切換圖片的效果,但是還是有一些局限,dom
排列只能是父級(jí)向上,不能把元素放在focus
元素的子元素里面。所以沒(méi)有js
靈活,但是代碼量更少。
focus-within 兼容性
因?yàn)?css3 的新增特性一直存在兼容問(wèn)題,這里查詢了一下它的兼容性,看到紅色區(qū)域還是不算太慘淡,出來(lái) ie,其他瀏覽器基本都支持了。
所有的源碼都可以在我的倉(cāng)庫(kù)地址:地址
到此這篇關(guān)于css3的focus-within選擇器的使用的文章就介紹到這了,更多相關(guān)css3的focus-within選擇器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何使用CSS3中的結(jié)構(gòu)偽類選擇器和偽元素選擇器
這篇文章主要介紹了詳解如何使用CSS3中的結(jié)構(gòu)偽類選擇器和偽元素選擇器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面2020-01-06- 這篇文章主要介紹了CSS3 新增選擇器的實(shí)例,需要的朋友可以參考下2019-11-13
css3 偽類選擇器快速?gòu)?fù)習(xí)小結(jié)
如果說(shuō) css 作為前端開(kāi)發(fā)的基本功, 那么 "選擇器" 就是基礎(chǔ)中的基礎(chǔ),本文介紹了css3 偽類選擇器快速?gòu)?fù)習(xí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工2019-09-10- CSS3 :default 偽類選擇器只能作用在表單元素上,表示默認(rèn)狀態(tài)的表單元素。接下來(lái)通過(guò)本文給大家介紹CSS3 :default偽類選擇器使用簡(jiǎn)介,感興趣的朋友跟隨腳本之家小編一起2018-03-15
css3之UI元素狀態(tài)偽類選擇器實(shí)例演示
這篇文章主要介紹了css3之UI元素狀態(tài)偽類選擇器 ,其中包括hover、active和focus,enabled,disabledread-only與read-write 等等,需要的朋友可以參考下2017-08-11CSS3選擇器新增問(wèn)題的實(shí)現(xiàn)
這篇文章主要介紹了CSS3選擇器新增問(wèn)題的實(shí)現(xiàn),本文給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-21