CSS display:none和visibility:hidden區(qū)別
你知道CSS display:none和visibility:hidden的區(qū)別嗎,這里和大家分享一下,使用CSS display:none屬性后,HTML元素(對象)的寬度、高度等各種屬性值都將“丟失”;而使用visibility:hidden屬性后,HTML元素(對象)僅僅是在視覺上看不見(完全透明),而它所占據(jù)的空間位置仍然存在。
CSS display:none和visibility:hidden的區(qū)別
visibility:hidden隱藏,但在瀏覽時保留位置;CSS display:none視為不存在,且不加載!
Overflow屬性值{visible|hidden|scroll|auto}前提是先要限制DIV的寬度(width)和高度(height)。二者都是隱藏HTML元素,在視覺效果上沒有區(qū)別,但在一些DOM操作中二者還是有所不同的。
CSS display:none;
使用該屬性后,HTML元素(對象)的寬度、高度等各種屬性值都將“丟失”;
visibility:hidden;
使用該屬性后,HTML元素(對象)僅僅是在視覺上看不見(完全透明),而它所占據(jù)的空間位置仍然存在,也即是說它仍具有高度、寬度等屬性值。
具體區(qū)別請看演示代碼吧:
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"lang="gb2312">
- <head>
- <head>
- <title>實例演示:CSS display:none和visible:hidden的區(qū)別</title>
- <metahttp-equivmetahttp-equiv="content-type"content="text/html;charset=gb2312"/>
- <metahttp-equivmetahttp-equiv="content-type"content="text/html;charset=gb2312"/>
- <metanamemetaname="author"content="楓巖,CnLei.y.l@gmail.com">
- <metanamemetaname="copyright"content="http://www.cnlei.com"/>
- </head>
- <body>
- <p><ahrefahref="javascript:alert($('CnLei_1').innerHTML+'的寬度:\n'
- +GetXYWH($('CnLei_1')).W);">點擊這里CSS display:none;</a></p>
- <p><ahrefahref="javascript:alert($('CnLei_2').innerHTML+'的寬度:\n'
- +GetXYWH($('CnLei_2')).W);">點擊這里visibility:hidden;</a></p>
- <dividdivid="CnLei_1"style="CSS display:none;">CnLei_1</div>
- <dividdivid="CnLei_2"style="visibility:hidden;">CnLei_2</div>
- <scripttypescripttype="text/javascript">
- varw3c=(document.getElementById)?true:false;
- varagt=navigator.userAgent.toLowerCase();
- varie=((agt.indexOf("msie")!=-1)
- &&(agt.indexOf("opera")==-1)&&(agt.indexOf("omniweb")==-1));
- varie5=(w3c&&ie)?true:false;
- varns6=(w3c&&(navigator.appName=="Netscape"))?true:false;
- function$(o){
- returndocument.getElementById(o)?document.getElementById(o):o;
- }
- functionGetXYWH(o){
- varo=$(o);
- varnLt=0;
- varnTop=0;
- varoffsetParent=o;
- while(offsetParent!=null&&offsetParent!=document.body){
- nLt+=offsetParent.offsetLeft;
- nTop+=offsetParent.offsetTop;
- if(!ns6){
- parseInt(offsetParent.currentStyle.borderLeftWidth)>0?
- nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
- parseInt(offsetParent.currentStyle.borderTopWidth)>0?
- nTop+=parseInt(offsetParent.currentStyle.borderTopWidth):"";
- }
- offsetParentoffsetParent=offsetParent.offsetParent;
- }
- return{X:nLt,Y:nTop,W:o.offsetWidth,H:o.offsetHeight};
- }
- </script>
- </body>
- </html>
【編輯推薦】
- 使用CSS display:none時注意事項
- CSS display:inline和float:left兩者區(qū)別
- 深入探究DIV CSS布局中position屬性用法
- CSS屬性display:inline-block使用揭秘
- CSS display:block顯示布局錯亂解決方案