濮阳杆衣贸易有限公司

主頁 > 知識庫 > 有趣的css實現(xiàn)隱藏元素的7種思路

有趣的css實現(xiàn)隱藏元素的7種思路

熱門標簽:電銷專用外呼線路 威力最大的電銷機器人 電銷外呼系統(tǒng)是違法的嗎 電話機器人鑰匙扣 漯河外呼調研線路 廣西房產(chǎn)智能外呼系統(tǒng)推薦 400電話唐山辦理 旅游地圖標注線路 地圖標注位置怎么弄圖

前言

display、visibility、opacity三個屬性隱藏元素之間的異同點一直是前端面試面試的常考題。

屬性 是否在頁面上顯示 注冊點擊事件是否有效 是否存在于可訪問性樹中
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>

參考

  • [1] display | MDN
  • [2] visibility | MDN
  • [3] opacity | MDN
  • [4] transform | MDN
  • [5] overflow | MDN
  • [6] color | MDN
  • [7] transform | MDN
  • [8] z-index | MDN
  • [9] CSS3 顏色值RGBA表示方式
  • [10] 一個也許很傻的問題,在圖像處理中alpha到底是什么?

到此這篇關于有趣的css實現(xiàn)隱藏元素的7種思路的文章就介紹到這了,更多相關css隱藏元素內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持腳本之家!

標簽:欽州 試駕邀約 焦作 綏化 湘西 無錫 銅陵 湖北

巨人網(wǎng)絡通訊聲明:本文標題《有趣的css實現(xiàn)隱藏元素的7種思路》,本文關鍵詞  有趣,的,css,實現(xiàn),隱藏,元素,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《有趣的css實現(xiàn)隱藏元素的7種思路》相關的同類信息!
  • 本頁收集關于有趣的css實現(xiàn)隱藏元素的7種思路的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    紫云| 西城区| 金堂县| 巩留县| 石景山区| 黔江区| 太康县| 宝丰县| 湖北省| 长沙县| 北安市| 镇宁| 英山县| 建瓯市| 屏东县| 临桂县| 新源县| 十堰市| 体育| 双鸭山市| 宜宾市| 庄浪县| 合阳县| 兰西县| 灵寿县| 汽车| 敦煌市| 彭泽县| 五台县| 斗六市| 周至县| 桃源县| 卢湾区| 陵川县| 广安市| 大港区| 茶陵县| 株洲县| 青海省| 徐水县| 西青区|