css3的focus-within選擇器的使用

偽元素和偽類
說到這個,我們先回顧一下,偽類選擇器和偽元素選擇器,老版的瀏覽器沒有嚴格區(qū)分下面 2 種寫法。
a:after{} a::after{}
在新的標準中,單冒號(:)用于 CSS3 偽類,雙冒號(::)用于 CSS3 偽元素,我們平時開發(fā)時可以注意一下,當然大多數(shù)瀏覽器兩種寫法都能識別。
常見偽元素和偽類
偽類
:link, :visited, :hover, :active, :focus, :first-child, :last-child, :nth-child, :nth-last-child, :not()
偽類一般用于一個元素的某個狀態(tài),比如說鼠標懸浮,按鈕點擊,鏈接已經(jīng)訪問,輸入框聚焦等,還用于選擇某個特殊元素,比如說多個元素中的第一個,最后一個,偶數(shù),奇數(shù)等。其作用是對某個符合以上條件的元素添加一些樣式。
a:hover{ text-decoration: underline; } a:active { color: blue; } a:link { color: red; } a:visited { color: green; }
上面的例子展示了一個a
標簽在不同狀態(tài)下的不同樣式,在未點擊鏈接之前,a
標簽呈現(xiàn)紅色字體(link),在鼠標移到a
標簽上是,a
標簽出現(xiàn)下劃線(hover),在鼠標按下的時候,a
標簽變?yōu)樗{色(active),點擊完了之后,a
標簽變?yōu)榫G色(visited)。可以看到,偽類的作用是為了給不同狀態(tài)的標簽添加樣式。
偽元素
::first-letter, ::first-line, ::before, ::after
在內(nèi)容模塊中提到,偽元素如果沒有設(shè)置“content”屬性,偽元素是無用的。使用偽元素插入的內(nèi)容在頁面的源碼里是不可見的,只能在 css 里可見。插入的元素在默認情況下是內(nèi)聯(lián)元素(或者,在 html5 中,在文本語義的類別里)。因此,為了給插入的元素賦予高度,填充,邊距等等,你通常必須顯式地定義它是一個塊級元素。還要注意的是典型的 CSS 繼承規(guī)則適用于插入的元素。例如,你有字體系列黑體,宋體,無襯線字體應(yīng)用到 body 元素里,然后偽元素會像其他元素一樣繼承這些字體系列。偽元素不會自然繼承自父元素(如 padding margins)的樣式。你的直覺是 :before 和 :after 偽元素可能是插入的內(nèi)容會被注入到目標元素的前或后注入。其實不是這樣的,注入的內(nèi)容將是有關(guān)聯(lián)的目標元素的子元素,但它會被置于這個元素的任何內(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>
運行效果:
可以看到,我們html
部分只寫了一個元素,但是我們利用偽元素渲染出來 3 個部分,前中后,這里我們可以認為,偽元素一般用來輔助html
的元素。但在內(nèi)容頁面的源碼又看不到,利用偽元素可以實現(xiàn)很多神奇的功能,這里不做具體講解,后面再出具體教程。
神奇的偽類:focus-within
言歸正傳,回到我們的主角focus-within
,我們知道,偽類focus
是指一個元素獲得焦點時,為其添加樣式。focus-within
的范圍更廣,它表示一個元素獲得焦點,或該元素的后代元素獲得焦點。劃重點,它或它的后代獲得焦點。這也就意味著,它或它的后代獲得焦點,都可以觸發(fā):focus-within
。
這個屬性有點類似Javascript
的事件冒泡,從可獲焦元素開始一直冒泡到根元素html
,都可以接收觸發(fā):focus-within
事件,類似下面這個簡單的例子這樣:
<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>
運行結(jié)果:
可以看到,在button
獲得焦點時,因為冒泡的原因,它的父級元素全部應(yīng)用了:focus-within
的樣式。這里值得注意的是,正常的div
是不能獲得焦點的,設(shè)置 tabindex 屬性才能獲取焦點,同時按鍵盤 Tab 鍵也可讓其獲取焦點,其中 tabindex 的值越小在 tab 鍵切換的時候就會首先聚焦。根據(jù):focus-within
的特性,我們在不利用 js 的情況下,實現(xiàn)很多實用性的功能。
感應(yīng)用戶聚焦區(qū)域
利用focus-within
可以增加用戶的感知區(qū)域,讓用戶獲得更好的視覺反饋。
<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>
可以看到在沒有任何javascript
邏輯控制情況下,用focus-within
就實現(xiàn)了上面的效果。
實現(xiàn)離屏導(dǎo)航
我們先看一下效果:
可以看到是一個很棒的導(dǎo)航效果,而且真?zhèn)€實現(xiàn)沒有使用javascript
控制,這無疑在性能和體驗上都有不少提升。具體源碼可以看下面的地址:地址
實現(xiàn) B 站,掘金等網(wǎng)站登錄動效切換
我們平時可能注意到了,B 站和掘金在用戶輸入密碼的時候,上面的圖片是捂著眼睛的,這里我們也可以用focus-within
來實現(xiàn)。
<html> <div class="g-wrap"></div> <div class="g-container"> <h2>登錄</h2> <div class="g-username"> <input maxlength="64" placeholder="請輸入手機號或郵箱" 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="請輸入密碼" 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
的情況下,也能實現(xiàn)動態(tài)切換圖片的效果,但是還是有一些局限,dom
排列只能是父級向上,不能把元素放在focus
元素的子元素里面。所以沒有js
靈活,但是代碼量更少。
focus-within 兼容性
因為 css3 的新增特性一直存在兼容問題,這里查詢了一下它的兼容性,看到紅色區(qū)域還是不算太慘淡,出來 ie,其他瀏覽器基本都支持了。
所有的源碼都可以在我的倉庫地址:地址
到此這篇關(guān)于css3的focus-within選擇器的使用的文章就介紹到這了,更多相關(guān)css3的focus-within選擇器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何使用CSS3中的結(jié)構(gòu)偽類選擇器和偽元素選擇器
這篇文章主要介紹了詳解如何使用CSS3中的結(jié)構(gòu)偽類選擇器和偽元素選擇器,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面2020-01-06- 這篇文章主要介紹了CSS3 新增選擇器的實例,需要的朋友可以參考下2019-11-13
- 如果說 css 作為前端開發(fā)的基本功, 那么 "選擇器" 就是基礎(chǔ)中的基礎(chǔ),本文介紹了css3 偽類選擇器快速復(fù)習(xí),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工2019-09-10
- CSS3 :default 偽類選擇器只能作用在表單元素上,表示默認狀態(tài)的表單元素。接下來通過本文給大家介紹CSS3 :default偽類選擇器使用簡介,感興趣的朋友跟隨腳本之家小編一起2018-03-15
- 這篇文章主要介紹了css3之UI元素狀態(tài)偽類選擇器 ,其中包括hover、active和focus,enabled,disabledread-only與read-write 等等,需要的朋友可以參考下2017-08-11
- 這篇文章主要介紹了CSS3選擇器新增問題的實現(xiàn),本文給大家介紹的非常詳細對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-21