校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

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

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

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

前言

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

屬性 是否在頁面上顯示 注冊點(diǎn)擊事件是否有效 是否存在于可訪問性樹中
display none
visibility hidden
opacity 0

除了display、visibility、opacity三個屬性可以隱藏元素之外,是否還存在其它屬性可以隱藏元素呢?它們之間又存在什么必然的聯(lián)系呢?這就是我們今天要討論的問題。

注:由于篇幅有限,本文并未提及一些像filter:alpha(opacity=0); zoom:0;之類的兼容屬性。

第一種:移除出可訪問性樹

display : none

display屬性可以設(shè)置元素的內(nèi)部和外部顯示類型。將display設(shè)置為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設(shè)置為hidden會使元素不可見,但此時元素仍然位于可訪問性樹中(display: none時元素被移出可訪問性樹 ),注冊點(diǎn)擊事件無效。

代碼:

<!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設(shè)置為0會使元素完全透明,此時元素不可見(因?yàn)樗峭该鞯?,仍然位于可訪問性樹中,注冊點(diǎn)擊事件有效。

代碼:

<!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設(shè)置為transparent(透明),此時元素不可見(因?yàn)樗峭该鞯?,仍然位于可訪問性樹中,注冊點(diǎn)擊事件有效。

代碼:

<!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)

從技術(shù)上說,transparent是 rgba(0,0,0,0) 的簡寫,將元素的background-color、color和border-color設(shè)置為rgba(0,0,0,0)(透明),此時元素不可見(因?yàn)樗峭该鞯?,仍然位于可訪問性樹中,注冊點(diǎn)擊事件有效。

代碼:

<!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即可達(dá)到隱藏元素的效果。

hsla(0,0%,0%,0)

hsla使用元素隱藏的機(jī)制與rgba一致,都是由第四個參數(shù)Alpha所控制的,將元素的background-color、color和border-color設(shè)置為hsla(0,0%,0%,0),此時元素不可見(因?yàn)樗峭该鞯?,仍然位于可訪問性樹中,注冊點(diǎn)擊事件有效。

代碼:

<!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即可達(dá)到隱藏元素的效果。

filter: opacity(0%)

filter(濾鏡) opacity(0% ~ 100%)轉(zhuǎn)化圖像的透明程度,值范圍于0%(完全透明) ~ 100%(完全不透明)之間。將元素的filter設(shè)置為opacity(0%),此時元素不可見(因?yàn)樗峭该鞯?,仍然位于可訪問性樹中,注冊點(diǎn)擊事件有效。

代碼:

<!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設(shè)置為scale(0, 0)會使元素在x軸和y軸上都縮放到0像素,此元素會顯示,也會占用位置,但是因?yàn)橐呀?jīng)縮放到0%,元素和內(nèi)容占用像素比為0*0,所以看不到此元素及其內(nèi)容,也無法點(diǎn)擊。

代碼:

<!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都設(shè)置為0,使元素占用像素比為0*0,但此時會出現(xiàn)兩種情況:
當(dāng)元素的display屬性為inline時,元素內(nèi)容會將元素寬高拉開;
當(dāng)元素的display屬性為block或inline-block時,元素寬高為0,但元素內(nèi)容依舊正常顯示,此時再加上overflow:hidden;即可裁剪掉元素外的元素內(nèi)容。

這個方法跟transform: scale(0,0)的不同點(diǎn)在于:transform: scale(0,0)是將元素與內(nèi)容都進(jìn)行縮放,而此方法是將元素縮放到0px,再裁剪掉元素外的元素內(nèi)容。

代碼:

<!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>

第五種:旋轉(zhuǎn)

transform: rotateX(90deg)

將元素沿著X軸順時針旋轉(zhuǎn)90度達(dá)到隱藏元素的效果。

代碼:

<!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軸順時針旋轉(zhuǎn)90度達(dá)到隱藏元素的效果。

代碼:

<!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>

第六種:脫離屏幕顯示位置

脫離屏幕顯示位置同樣可以使元素不可見,但是達(dá)到這種效果的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>

第七種:遮蓋

使用元素遮蓋也可以使元素不可見,因?yàn)檫_(dá)到這種效果的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到底是什么?

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

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

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《有趣的css實(shí)現(xiàn)隱藏元素的7種思路》,本文關(guān)鍵詞  有趣,的,css,實(shí)現(xiàn),隱藏,元素,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《有趣的css實(shí)現(xiàn)隱藏元素的7種思路》相關(guān)的同類信息!
  • 本頁收集關(guān)于有趣的css實(shí)現(xiàn)隱藏元素的7種思路的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 瑞丽市| 林州市| 武定县| 东宁县| 五家渠市| 锡林浩特市| 芮城县| 旌德县| 准格尔旗| 宁远县| 鹤庆县| 商都县| 江安县| 香格里拉县| 和林格尔县| 迭部县| 乡城县| 万山特区| 南溪县| 靖远县| 茂名市| 龙游县| 庆云县| 若尔盖县| 娄底市| 安乡县| 来宾市| 井研县| 青铜峡市| 靖边县| 女性| 敦煌市| 乌拉特前旗| 武宣县| 红河县| 南靖县| 宿松县| 南投市| 大同市| 高尔夫| 清丰县|