這篇文章主要介紹css3中focus-within選擇器怎么用,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),封丘網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:封丘等地區(qū)。封丘做網(wǎng)站價格咨詢:028-86922220
偽元素和偽類
說到這個,我們先回顧一下,偽類選擇器和偽元素選擇器,老版的瀏覽器沒有嚴(yán)格區(qū)分下面 2 種寫法。
a:after{} a::after{}
在新的標(biāo)準(zhǔn)中,單冒號(:)用于 CSS3 偽類,雙冒號(::)用于 CSS3 偽元素,我們平時開發(fā)時可以注意一下,當(dāng)然大多數(shù)瀏覽器兩種寫法都能識別。
常見偽元素和偽類
偽類
:link, :visited, :hover, :active, :focus, :first-child, :last-child, :nth-child, :nth-last-child, :not()
偽類一般用于一個元素的某個狀態(tài),比如說鼠標(biāo)懸浮,按鈕點(diǎn)擊,鏈接已經(jīng)訪問,輸入框聚焦等,還用于選擇某個特殊元素,比如說多個元素中的第一個,最后一個,偶數(shù),奇數(shù)等。其作用是對某個符合以上條件的元素添加一些樣式。
a:hover{ text-decoration: underline; } a:active { color: blue; } a:link { color: red; } a:visited { color: green; }
上面的例子展示了一個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)按下的時候,a
標(biāo)簽變?yōu)樗{(lán)色(active),點(diǎn)擊完了之后,a
標(biāo)簽變?yōu)榫G色(visited)??梢钥吹剑瑐晤惖淖饔檬菫榱私o不同狀態(tài)的標(biāo)簽添加樣式。
偽元素
::first-letter, ::first-line, ::before, ::after
在內(nèi)容模塊中提到,偽元素如果沒有設(shè)置“content”屬性,偽元素是無用的。使用偽元素插入的內(nèi)容在頁面的源碼里是不可見的,只能在 css 里可見。插入的元素在默認(rèn)情況下是內(nèi)聯(lián)元素(或者,在 html5 中,在文本語義的類別里)。因此,為了給插入的元素賦予高度,填充,邊距等等,你通常必須顯式地定義它是一個塊級元素。還要注意的是典型的 CSS 繼承規(guī)則適用于插入的元素。例如,你有字體系列黑體,宋體,無襯線字體應(yīng)用到 body 元素里,然后偽元素會像其他元素一樣繼承這些字體系列。偽元素不會自然繼承自父元素(如 padding margins)的樣式。你的直覺是 :before 和 :after 偽元素可能是插入的內(nèi)容會被注入到目標(biāo)元素的前或后注入。其實(shí)不是這樣的,注入的內(nèi)容將是有關(guān)聯(lián)的目標(biāo)元素的子元素,但它會被置于這個元素的任何內(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
部分只寫了一個元素,但是我們利用偽元素渲染出來 3 個部分,前中后,這里我們可以認(rèn)為,偽元素一般用來輔助html
的元素。但在內(nèi)容頁面的源碼又看不到,利用偽元素可以實(shí)現(xiàn)很多神奇的功能,這里不做具體講解,后面再出具體教程。
神奇的偽類:focus-within
言歸正傳,回到我們的主角focus-within
,我們知道,偽類focus
是指一個元素獲得焦點(diǎn)時,為其添加樣式。focus-within
的范圍更廣,它表示一個元素獲得焦點(diǎn),或該元素的后代元素獲得焦點(diǎn)。劃重點(diǎn),它或它的后代獲得焦點(diǎn)。這也就意味著,它或它的后代獲得焦點(diǎn),都可以觸發(fā):focus-within
。
這個屬性有點(diǎn)類似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>
運(yùn)行結(jié)果:
可以看到,在button
獲得焦點(diǎn)時,因?yàn)槊芭莸脑?,它的父級元素全部?yīng)用了:focus-within
的樣式。這里值得注意的是,正常的div
是不能獲得焦點(diǎn)的,設(shè)置 tabindex 屬性才能獲取焦點(diǎn),同時按鍵盤 Tab 鍵也可讓其獲取焦點(diǎn),其中 tabindex 的值越小在 tab 鍵切換的時候就會首先聚焦。根據(jù):focus-within
的特性,我們在不利用 js 的情況下,實(shí)現(xiàn)很多實(shí)用性的功能。
感應(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
就實(shí)現(xiàn)了上面的效果。
實(shí)現(xiàn)離屏導(dǎo)航
我們先看一下效果:
可以看到是一個很棒的導(dǎo)航效果,而且真?zhèn)€實(shí)現(xiàn)沒有使用javascript
控制,這無疑在性能和體驗(yàn)上都有不少提升。具體源碼可以看下面的地址:地址
實(shí)現(xiàn) B 站,掘金等網(wǎng)站登錄動效切換
我們平時可能注意到了,B 站和掘金在用戶輸入密碼的時候,上面的圖片是捂著眼睛的,這里我們也可以用focus-within
來實(shí)現(xiàn)。
<html> <div class="g-wrap"></div> <div class="g-container"> <h3>登錄</h3> <div class="g-username"> <input maxlength="64" placeholder="請輸入手機(jī)號或郵箱" class="input"> <img src="/upload/otherpic49/greeting.1415c1c.png" class="g-username"> </div> <div class="g-password"> <input type="password" maxlength="64" placeholder="請輸入密碼" class="input"> <img src="/upload/otherpic49/blindfold.58ce423.png" class="g-password"> </div> <img src="/upload/otherpic49/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 h3 { 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)動態(tài)切換圖片的效果,但是還是有一些局限,dom
排列只能是父級向上,不能把元素放在focus
元素的子元素里面。所以沒有js
靈活,但是代碼量更少。
focus-within 兼容性
因?yàn)?css3 的新增特性一直存在兼容問題,這里查詢了一下它的兼容性,看到紅色區(qū)域還是不算太慘淡,出來 ie,其他瀏覽器基本都支持了。
以上是“css3中focus-within選擇器怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享文章:css3中focus-within選擇器怎么用
文章URL:http://bm7419.com/article36/goscpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航、商城網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)