更加不错的弹出层,相对居中

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. <style>
  7. * {margin:0;}
  8. body {font-size:12px;}
  9. #container div{margin-top:5px;}
  10. #select_city h3{float:left;}
  11. #main_city,#all_province {clear:both;}
  12. #main_city div,#all_province div{width:600px;clear:left;}
  13. #main_city h4,#all_province h4{float:left;display:inline;font-size:16px;}
  14. #main_city div span,#all_province div span{margin:0 10px;cursor:pointer;font-size:12px;}
  15. #float_lay{width:200px;height:120px;border:1px #d1d1d1 solid;position:absolute;background:#fff;z-index:999;display:none;}
  16. </style>
  17. <script type="text/javascript">
  18. //弹出层
  19. function openLayer(objId,conId){
  20. var arrayPageSize = getPageSize();//调用getPageSize()函数
  21. var arrayPageScroll = getPageScroll();//调用getPageScroll()函数
  22. if (!document.getElementById("popupAddr")){
  23. //创建弹出内容层
  24. var popupDiv = document.createElement("div");
  25. //给这个元素设置属性与样式
  26. popupDiv.setAttribute("id","popupAddr")
  27. popupDiv.style.position = "absolute";
  28. popupDiv.style.border = "1px solid #ccc";
  29. popupDiv.style.background = "#fff";
  30. popupDiv.style.zIndex = 99;
  31.  
  32. //创建弹出背景层
  33. var bodyBack = document.createElement("div");
  34. bodyBack.setAttribute("id","bodybg")
  35. bodyBack.style.position = "absolute";
  36. bodyBack.style.width = "100%";
  37. bodyBack.style.height = (arrayPageSize[1] + 35 + 'px');
  38. bodyBack.style.zIndex = 98;
  39. bodyBack.style.top = 0;
  40. bodyBack.style.left = 0;
  41. bodyBack.style.filter = "alpha(opacity=50)";
  42. bodyBack.style.opacity = 0.5;
  43. bodyBack.style.background = "#ddf";
  44. //实现弹出(插入到目标元素之后)
  45. var mybody = document.getElementById(objId);
  46. insertAfter(popupDiv,mybody);//执行函数insertAfter()
  47. insertAfter(bodyBack,mybody);//执行函数insertAfter()
  48. }
  49. //显示背景层
  50. document.getElementById("bodybg").style.display = "";
  51. //显示内容层
  52. var popObj=document.getElementById("popupAddr")
  53. popObj.innerHTML = document.getElementById(conId).innerHTML;
  54. popObj.style.display = "";
  55. //让弹出层在页面中垂直左右居中(统一)
  56. //popObj.style.width = "600px";
  57. //popObj.style.height = "400px";
  58. //popObj.style.top = arrayPageScroll[1] + (arrayPageSize[3] - 35 - 400) / 2 + 'px';
  59. //popObj.style.left = (arrayPageSize[0] - 20 - 600) / 2 + 'px';
  60. //让弹出层在页面中垂直左右居中(个性)
  61. var arrayConSize=getConSize(conId)
  62. popObj.style.top = arrayPageScroll[1] + (arrayPageSize[3] - arrayConSize[1]) / 2-50 + 'px';
  63. popObj.style.left = (arrayPageSize[0] - arrayConSize[0]) / 2 -30 + 'px';
  64. }
  65. //获取内容层内容原始尺寸
  66. function getConSize(conId){
  67. var conObj=document.getElementById(conId)
  68. conObj.style.position = "absolute";
  69. conObj.style.left=-1000+"px";
  70. conObj.style.display="";
  71. var arrayConSize=[conObj.offsetWidth,conObj.offsetHeight]
  72. conObj.style.display="none";
  73. return arrayConSize;
  74. }
  75. function insertAfter(newElement,targetElement){//插入
  76. var parent = targetElement.parentNode;
  77. if(parent.lastChild == targetElement){
  78. parent.appendChild(newElement);
  79. }
  80. else{
  81. parent.insertBefore(newElement,targetElement.nextSibling);
  82. }
  83. }
  84. //获取滚动条的高度
  85. function getPageScroll(){
  86. var yScroll;
  87. if (self.pageYOffset) {
  88. yScroll = self.pageYOffset;
  89. } else if (document.documentElement && document.documentElement.scrollTop){
  90. yScroll = document.documentElement.scrollTop;
  91. } else if (document.body) {
  92. yScroll = document.body.scrollTop;
  93. }
  94. arrayPageScroll = new Array('',yScroll)
  95. return arrayPageScroll;
  96. }
  97. //获取页面实际大小
  98. function getPageSize(){
  99. var xScroll,yScroll;
  100. if (window.innerHeight && window.scrollMaxY){
  101. xScroll = document.body.scrollWidth;
  102. yScroll = window.innerHeight + window.scrollMaxY;
  103. } else if (document.body.scrollHeight > document.body.offsetHeight){
  104. sScroll = document.body.scrollWidth;
  105. yScroll = document.body.scrollHeight;
  106. } else {
  107. xScroll = document.body.offsetWidth;
  108. yScroll = document.body.offsetHeight;
  109. }
  110. var windowWidth,windowHeight;
  111. //var pageHeight,pageWidth;
  112. if (self.innerHeight) {
  113. windowWidth = self.innerWidth;
  114. windowHeight = self.innerHeight;
  115. } else if (document.documentElement && document.documentElement.clientHeight) {
  116. windowWidth = document.documentElement.clientWidth;
  117. windowHeight = document.documentElement.clientHeight;
  118. } else if (document.body) {
  119. windowWidth = document.body.clientWidth;
  120. windowHeight = document.body.clientHeight;
  121. }
  122. var pageWidth,pageHeight
  123. if(yScroll < windowHeight){
  124. pageHeight = windowHeight;
  125. } else {
  126. pageHeight = yScroll;
  127. }
  128. if(xScroll < windowWidth) {
  129. pageWidth = windowWidth;
  130. } else {
  131. pageWidth = xScroll;
  132. }
  133. arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  134. return arrayPageSize;
  135. }
  136. //关闭弹出层
  137. function closeLayer(){
  138. document.getElementById("popupAddr").style.display = "none";
  139. document.getElementById("bodybg").style.display = "none";
  140. return false;
  141. }
  142. </script>
  143. </head>
  144. <body>
  145. <input name="Input" id="test" value="风格设置" type="button" onclick="openLayer('test','test_con')" />
  146. <!--第一个弹出层的内容-->
  147. <div id="test_con" style="display:none" title="包含弹出层的内容的容器">
  148. <div id="tab">
  149. <div id="tabtop">
  150. <div id="tabtop-L"><strong>风格设置</strong></div>
  151. <div id="tabtop-R" onclick="closeLayer()"><strong>关闭</strong></div>
  152. </div>
  153. <div id="tabcontent">此处显示内容</div>
  154. </div>
  155. </div>
  156. <br>
  157. <br>
  158. <br>
  159. <input name="Input" id="test2" value="风格提示" type="button" onclick="openLayer('test2','test_con2')" />
  160. <!--第二个弹出层的内容-->
  161. <div id="test_con2" style="display:none" title="包含弹出层的内容的容器">
  162. <div id="tab2">
  163. <div id="tabtop2">
  164. <div id="tabtop-L2"><strong style="color:blue">设置提示</strong></div>
  165. <form name='f'>
  166.     name:<input type="text" name="n">
  167.     pwd:<input type="password" name="pwd">
  168.     <input type="submit">
  169.     
  170. </form>
  171. <div id="tabtop-R2" onclick="closeLayer()"><strong style="color:red">关闭</strong></div>
  172. </div>
  173. <div id="tabcontent2" style="color:#dff;background:#000">
  174. <p>此处显示提示内容"
  175. <p>提示1
  176. <p>提示2
  177. </div>
  178. </div>
  179. </div>
  180. <br>
  181. <br>
  182. <br>
  183. <input name="Input" id="test3" value="风格演示" type="button" onclick="openLayer('test3','test_con3')" />
  184. <!--第三个弹出层的内容-->
  185. <div id="test_con3" style="display:none" title="包含弹出层的内容的容器">
  186. <div id="tab3" style="width:300px;height:180px;background:#fee">
  187. <div id="tabtop3">
  188. <div id="tabtop-L3"><strong style="color:red;font-size:20px">风格演示</strong></div>
  189. <div id="tabtop-R3" onclick="closeLayer()"><strong>关闭</strong></div>
  190. </div>
  191. <div id="tabcontent3">此处显示内容</div>
  192. </div>
  193. </div>
  194. </body>
  195. </html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值