屬性 | 值 | 是否在頁面上顯示 | 注冊點擊事件是否有效 | 是否存在于可訪問性樹中 |
---|---|---|---|---|
display | none | 否 | 否 | 否 |
visibility | hidden | 否 | 否 | 是 |
opacity | 0 | 否 | 是 | 是 |
除了display、visibility、opacity三個屬性可以隱藏元素之外,是否還存在其它屬性可以隱藏元素呢?它們之間又存在什么必然的聯(lián)系呢?這就是我們今天要討論的問題。
注:由于篇幅有限,本文并未提及一些像filter:alpha(opacity=0); zoom:0;之類的兼容屬性。
display : none
display屬性可以設置元素的內部和外部顯示類型。將display設置為none會將元素從可訪問性樹中移除。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>display : none</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { display : none; } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
visibility: hidden
將visibility設置為hidden會使元素不可見,但此時元素仍然位于可訪問性樹中(display: none時元素被移出可訪問性樹 ),注冊點擊事件無效。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>visibility: hidden</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { visibility: hidden; } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
opacity: 0
opacity(不透明度),取值范圍0(完全透明) ~ 1(完全不透明),將opacity設置為0會使元素完全透明,此時元素不可見(因為它是透明的),仍然位于可訪問性樹中,注冊點擊事件有效。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>opacity: 0</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { opacity: 0; } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
transparent
將元素的background-color、color和border-color設置為transparent(透明),此時元素不可見(因為它是透明的),仍然位于可訪問性樹中,注冊點擊事件有效。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>transparent</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { color: transparent; background-color: transparent; border-color: transparent; } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
rgba(0,0,0,0)
從技術上說,transparent是 rgba(0,0,0,0) 的簡寫,將元素的background-color、color和border-color設置為rgba(0,0,0,0)(透明),此時元素不可見(因為它是透明的),仍然位于可訪問性樹中,注冊點擊事件有效。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>rgba(0,0,0,0)</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { color: rgba(0,0,0,0); background-color: rgba(0,0,0,0); border-color: rgba(0,0,0,0); } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
rgba只需要第四個參數(shù)為0即可達到隱藏元素的效果。
hsla(0,0%,0%,0)
hsla使用元素隱藏的機制與rgba一致,都是由第四個參數(shù)Alpha所控制的,將元素的background-color、color和border-color設置為hsla(0,0%,0%,0),此時元素不可見(因為它是透明的),仍然位于可訪問性樹中,注冊點擊事件有效。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>hsla(0,0%,0%,0)</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { color: hsla(0,0%,0%,0); background-color: hsla(0,0%,0%,0); border-color: hsla(0,0%,0%,0); } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
hsla和rgba一致,只需要第四個參數(shù)為0即可達到隱藏元素的效果。
filter: opacity(0%)
filter(濾鏡) opacity(0% ~ 100%)轉化圖像的透明程度,值范圍于0%(完全透明) ~ 100%(完全不透明)之間。將元素的filter設置為opacity(0%),此時元素不可見(因為它是透明的),仍然位于可訪問性樹中,注冊點擊事件有效。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>filter: opacity(0%)</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { filter: opacity(0%); } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
transform: scale(0, 0)
將transform設置為scale(0, 0)會使元素在x軸和y軸上都縮放到0像素,此元素會顯示,也會占用位置,但是因為已經(jīng)縮放到0%,元素和內容占用像素比為0*0,所以看不到此元素及其內容,也無法點擊。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>transform: scale(0, 0)</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { transform: scale(0,0); } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
width: 0;height: 0;overflow: hidden
將width和height都設置為0,使元素占用像素比為0*0,但此時會出現(xiàn)兩種情況:
當元素的display屬性為inline時,元素內容會將元素寬高拉開;
當元素的display屬性為block或inline-block時,元素寬高為0,但元素內容依舊正常顯示,此時再加上overflow:hidden;即可裁剪掉元素外的元素內容。
這個方法跟transform: scale(0,0)的不同點在于:transform: scale(0,0)是將元素與內容都進行縮放,而此方法是將元素縮放到0px,再裁剪掉元素外的元素內容。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>width: 0;height: 0;overflow: hidden</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { width:0; height:0; overflow: hidden; border-width: 0;/* user agent stylesheet中border-width: 2px; */ padding: 0;/* user agent stylesheet中padding: 1px 6px; */ } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
transform: rotateX(90deg)
將元素沿著X軸順時針旋轉90度達到隱藏元素的效果。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>transform: rotateX(90deg)</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { transform: rotateX(90deg); } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
transform: rotateY(90deg)
將元素沿著Y軸順時針旋轉90度達到隱藏元素的效果。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>transform: rotateY(90deg)</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { transform: rotateY(90deg); } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
脫離屏幕顯示位置同樣可以使元素不可見,但是達到這種效果的css樣式太多了,這里只舉例一種情況說明。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>脫離屏幕顯示位置</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { position: fixed; top: -100px; left: -100px; } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div> <button id="bt">按鈕</button> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
使用元素遮蓋也可以使元素不可見,因為達到這種效果的css樣式也很多,故這里只舉例一種情況說明。
代碼:
<!DOCTYPE html> <html> <head> <meta name="charset" content="utf-8"/> <title>遮蓋</title> <style type="text/css"> div { background-color: red; width: 100px; height: 100px; line-height: 100px; text-align: center; margin-top: 24px; } button { background-color: black; color: white; } #bt { z-index: -1; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); } #cover { z-index: 1; position: absolute; top: 0; left: 0; margin: 0; } </style> </head> <body> <div> <button id="normal">按鈕</button> </div> <div style="position: relative;line-height: normal;"> <button id="bt">按鈕</button> <div id="cover"></div> </div> <script type="text/javascript"> let normal = document.getElementById('normal'); let bt = document.getElementById('bt'); normal.addEventListener('click',function(){ alert('click normal'); }) bt.addEventListener('click',function(){ alert('click bt'); }) </script> </body> </html>
到此這篇關于有趣的css實現(xiàn)隱藏元素的7種思路的文章就介紹到這了,更多相關css隱藏元素內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持腳本之家!