点击后弹出遮罩层,居中显示,兼容火狐浏览器。网页上已经有很多类似的代码了,使用挺广泛的,代码先判断浏览器的版本,ie6创建的div样式和非ie6创建的div样式在解析时有点区别,为了兼容性考虑,才加了判断。 经过烈火小编测试,可以完美兼容IE系列及火狐浏览器,只是没有关闭层的按钮,需要点击层以外的任意空白关闭。 演示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>点击弹出居中的遮罩层代码</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <style type="text/css"> .hidden{display:none;} #smallLay{width:498px; height:100px;padding:4px 10px 10px;background-color:#FFFFFF;border:1px solid #05549d;color:#333333;line-height:24px;text-align:left;-webkit-box-shadow:5px 2px 6px #000;-moz-box-shadow:3px 3px 6px #555;} </style> <script type="text/javascript"> function showid(idname){ var isIE = (document.all) ? true : false; var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6); var newbox=document.getElementById(idname); newbox.style.zIndex="9999"; newbox.style.display="block" newbox.style.position = !isIE6 ? "fixed" : "absolute"; newbox.style.top =newbox.style.left = "50%"; newbox.style.marginTop = - newbox.offsetHeight / 2 + "px"; newbox.style.marginLeft = - newbox.offsetWidth / 2 + "px"; var layer=document.createElement("div"); layer.id="layer"; layer.style.width=layer.style.height="100%"; layer.style.position= !isIE6 ? "fixed" : "absolute"; layer.style.top=layer.style.left=0; layer.style.backgroundColor="#000"; layer.style.zIndex="9998"; layer.style.opacity="0.6"; document.body.appendChild(layer); var sel=document.getElementsByTagName("select"); for(var i=0;i<sel.length;i++){ sel[i].style.visibility="hidden"; } function layer_iestyle(){ layer.style.width=Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px"; layer.style.height= Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px"; } // liehuo.net function newbox_iestyle(){ newbox.style.marginTop = document.documentElement.scrollTop - newbox.offsetHeight / 2 + "px"; newbox.style.marginLeft = document.documentElement.scrollLeft - newbox.offsetWidth / 2 + "px"; } if(isIE){layer.style.filter ="alpha(opacity=60)";} if(isIE6){ layer_iestyle() newbox_iestyle(); window.attachEvent("onscroll",function(){ newbox_iestyle(); }) window.attachEvent("onresize",layer_iestyle) } layer.onclick=function(){newbox.style.display="none";layer.style.display="none";for(var i=0;i<sel.length;i++){ sel[i].style.visibility="visible"; }} } </script> </head> <body> <input type="button" id="showbtn" name="showbtn" value="点我弹出层" onclick="showid('smallLay');"> <div id="smallLay" style="display:none;">啦啦,我是弹出的遮罩层,我比较坏,没有关闭按钮!嘿嘿,点击层以外的地方关闭哦。。烈火網 liehuo.net 欢迎复制,拒绝恶意采集 liehuo.net</div> </body> </html> <br /><center>如不能显示效果,请按Ctrl+F5刷新本页,更多网页代码:<a href='http://www.veryhuo.com/' target='_blank'>http://www.veryhuo.com/</a></center>提示:可修改后代码再运行! 文章导航 CSS完成未知宽高的图片水平、垂直居中代码代码简洁的模态窗口,可拖动、拽的层代码