原文:http://xutao5641745.iteye.com/blog/1310985
上一篇是关于遮罩层的展示: 链接地址为:http://xutao5641745.iteye.com/blog/1310955
1.让层始终显示在屏幕正中间:
样式代码:
- .model{
- position: absolute; z-index: 1003;
- 320px; height:320px; text-align:center;
- background-color:#0066FF; display: none;
- }
.model{ position: absolute; z-index: 1003; 320px; height:320px; text-align:center; background-color:#0066FF; display: none; }
jquery代码:
- //让指定的DIV始终显示在屏幕正中间
- function letDivCenter(divName){
- var top = ($(window).height() - $(divName).height())/2;
- var left = ($(window).width() - $(divName).width())/2;
- var scrollTop = $(document).scrollTop();
- var scrollLeft = $(document).scrollLeft();
- $(divName).css( { position : 'absolute', 'top' : top + scrollTop, left : left + scrollLeft } ).show();
- }
//让指定的DIV始终显示在屏幕正中间 function letDivCenter(divName){ var top = ($(window).height() - $(divName).height())/2; var left = ($(window).width() - $(divName).width())/2; var scrollTop = $(document).scrollTop(); var scrollLeft = $(document).scrollLeft(); $(divName).css( { position : 'absolute', 'top' : top + scrollTop, left : left + scrollLeft } ).show(); }
html代码:
- <a href="javascript:;" onclick="letDivCenter('#model')">点我让DIV始终显示在屏幕中间</a><br />
- <div>
- <div id="model" class="model">
- 这是内容(不过没有垂直居中显示)希望各位高手,能够补充。小弟在此谢过了。
- </div>
- </div>
<a href="javascript:;" onclick="letDivCenter('#model')">点我让DIV始终显示在屏幕中间</a><br /> <div> <div id="model" class="model"> 这是内容(不过没有垂直居中显示)希望各位高手,能够补充。小弟在此谢过了。 </div> </div>
运行一下看看效果吧。
接下来总结一下,将它们整合成一个。即,当弹出div层的时候,同时也要弹出遮罩层,好,废话不多说,看代码:
1。CSS样式:
- <style type="text/css">
- .mask {
- position: absolute; top: 0px; filter: alpha(opacity=60); background-color: #777;
- z-index: 1002; left: 0px;
- opacity:0.5; -moz-opacity:0.5;
- }
- .model{
- position: absolute; z-index: 1003;
- 320px; height:320px; text-align:center;
- background-color:#0066FF; display: none;
- }
- </style>
<style type="text/css"> .mask { position: absolute; top: 0px; filter: alpha(opacity=60); background-color: #777; z-index: 1002; left: 0px; opacity:0.5; -moz-opacity:0.5; } .model{ position: absolute; z-index: 1003; 320px; height:320px; text-align:center; background-color:#0066FF; display: none; } </style>
2。Jquery代码:
- <script type="text/javascript">
- //兼容火狐、IE8
- function showMask(){
- $("#mask").css("height",$(document).height());
- $("#mask").css("width",$(document).width());
- $("#mask").show();
- }
- //让指定的DIV始终显示在屏幕正中间
- function letDivCenter(divName){
- var top = ($(window).height() - $(divName).height())/2;
- var left = ($(window).width() - $(divName).width())/2;
- var scrollTop = $(document).scrollTop();
- var scrollLeft = $(document).scrollLeft();
- $(divName).css( { position : 'absolute', 'top' : top + scrollTop, left : left + scrollLeft } ).show();
- }
- function showAll(divName){
- showMask();
- letDivCenter(divName);
- }
- </script>
<script type="text/javascript"> //兼容火狐、IE8 function showMask(){ $("#mask").css("height",$(document).height()); $("#mask").css("width",$(document).width()); $("#mask").show(); } //让指定的DIV始终显示在屏幕正中间 function letDivCenter(divName){ var top = ($(window).height() - $(divName).height())/2; var left = ($(window).width() - $(divName).width())/2; var scrollTop = $(document).scrollTop(); var scrollLeft = $(document).scrollLeft(); $(divName).css( { position : 'absolute', 'top' : top + scrollTop, left : left + scrollLeft } ).show(); } function showAll(divName){ showMask(); letDivCenter(divName); } </script>
3.HTML代码:
- <div id="mask" class="mask"></div>
- <a href="javascript:;" onclick="showMask()" >点我显示遮罩层</a><br />
- <a href="javascript:;" onclick="letDivCenter('#model')">点我让DIV始终显示在屏幕中间</a><br />
- <a href="javascript:;" onclick="showAll('#model')">点我显示所有</a><br />
- <div>
- <div id="model" class="model">
- 这是内容(不过没有垂直居中显示)希望各位高手,能够补充。小弟在此谢过了。
- </div>
- </div>
<div id="mask" class="mask"></div> <a href="javascript:;" onclick="showMask()" >点我显示遮罩层</a><br /> <a href="javascript:;" onclick="letDivCenter('#model')">点我让DIV始终显示在屏幕中间</a><br /> <a href="javascript:;" onclick="showAll('#model')">点我显示所有</a><br /> <div> <div id="model" class="model"> 这是内容(不过没有垂直居中显示)希望各位高手,能够补充。小弟在此谢过了。 </div> </div>