好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

有趣的css實現隱藏元素的7種思路

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

前言

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

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

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

注:由于篇幅有限,本文并未提及一些像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只需要第四個參數為0即可達到隱藏元素的效果。

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

hsla使用元素隱藏的機制與rgba一致,都是由第四個參數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一致,只需要第四個參數為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像素,此元素會顯示,也會占用位置,但是因為已經縮放到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,但此時會出現兩種情況:
當元素的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實現隱藏元素的7種思路的文章就介紹到這了,更多相關css隱藏元素內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章,希望大家以后多多支持腳本之家!

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

巨人網絡通訊聲明:本文標題《有趣的css實現隱藏元素的7種思路》,本文關鍵詞  有趣,的,css,實現,隱藏,元素,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《有趣的css實現隱藏元素的7種思路》相關的同類信息!
  • 本頁收集關于有趣的css實現隱藏元素的7種思路的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 毛片免费的| bennettbrea大战黑人| 久久无码人妻精品一区二区三区| 白袜奴gay网站| 九九热伊人| 贵妃挨脔日常NP| 极品丰满国模冰莲大尺度| 女人扒开下面让男人桶爽视频| 国产精品午夜福利在线观看| 女人脱了内裤让男人猛戳视频| 被啪羞羞视频在线观看| 中文字幕第二页精品手机视频在线 | 91桃色在线观看| 被夫上司强迫的女人在线| 成人免费看AA片jPg| 一级a一级a爰片免费免丅快三| 青年的调教小说| 柳州莫菁门久久久久久久| 国产乡下妇女做爰视频| 艳母动漫在线观看免费| 中国欧美日韩一区二区三区 | 美女岳肉太深了使劲| 国产成人小视频| 男女一边摸一边做爽爽的动漫视频 | 成人黄色a级片| 印度性xxxx| 真人真做免费60分钟| 99成人??国产精品视频| 欧美videos巨大18free| 一级黄色a毛片| 人人看人人草| 太紧了灬太粗太硬了放轻松| 国产伦精品一区二区三区网站| 美人诱受多汁水肉np| 揉?啊?嗯~出水了男同动漫软件| ?国产嫩草影院?新年舞蹈| 男生捅女生逼| 日韩视频中文字幕| 伊人久久五月| 精品人妻无码一区二区出白浆潮喷| 午夜精品一区|