温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

CSS中display:none和visibility:hidden的区别是什么

发布时间:2021-07-09 16:31:58 来源:亿速云 阅读:137 作者:Leah 栏目:web开发

CSS中display:none和visibility:hidden的区别是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

CSS display:none和visibility:hidden的区别

visibility:hidden隐藏,但在浏览时保留位置;CSS display:none视为不存在,且不加载!

Overflow属性值{visible|hidden|scroll|auto}前提是先要限制DIV的宽度(width)和高度(height)。二者都是隐藏HTML元素,在视觉效果上没有区别,但在一些DOM操作中二者还是有所不同的。

CSS display:none;

使用该属性后,HTML元素(对象)的宽度、高度等各种属性值都将“丢失”;

visibility:hidden;

使用该属性后,HTML元素(对象)仅仅是在视觉上看不见(完全透明),而它所占据的空间位置仍然存在,也即是说它仍具有高度、宽度等属性值。

具体区别请看演示代码吧:

<!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的区别</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和visibility:hidden的区别是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI