HTML实现遮罩层的方法 HTML中如何使用遮罩层

所属分类: 网页制作 / HTML/Xhtml 阅读数: 847
收藏 0 赞 0 分享

Web页面中使用遮罩层,可防止重复操作,提示loading;也可以模拟弹出模态窗口。

实现思路:一个DIV作为遮罩层,一个DIV显示loading动态GIF图片。在下面的示例代码中,同时展示了如何在iframe子页面中调用显示和隐藏遮罩层。

示例代码:

index.html

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>  
  2. <html lang="zh-CN">  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <meta http-equiv="X-UA-Commpatible" content="IE=edge">  
  6. <title>HTML遮罩层</title>  
  7. <link rel="stylesheet" href="css/index.css">  
  8. </head>  
  9. <body>  
  10.     <div class="header" id="header">  
  11.         <div class="title-outer">  
  12.             <span class="title">  
  13.                 HTML遮罩层使用   
  14.             </span>  
  15.         </div>  
  16.     </div>  
  17.     <div class="body" id="body">  
  18.         <iframe id="iframeRight" name="iframeRight" width="100%" height="100%"  
  19.             scrolling="no" frameborder="0"  
  20.             style="border: 0px;margin: 0px; padding: 0px; width: 100%; height: 100%;overflow: hidden;"  
  21.             onload="rightIFrameLoad(this)" src="body.html"></iframe>  
  22.     </div>  
  23.        
  24.     <!-- 遮罩层DIV -->  
  25.     <div id="overlay" class="overlay"></div>  
  26.     <!-- Loading提示 DIV -->  
  27.     <div id="loadingTip" class="loading-tip">  
  28.         <img src="images/loading.gif" />  
  29.     </div>  
  30.        
  31.     <!-- 模拟模态窗口DIV -->  
  32.     <div class="modal" id="modalDiv"></div>  
  33.        
  34.     <script type='text/javascript' src="js/jquery-1.10.2.js"></script>  
  35.     <script type="text/javascript" src="js/index.js"></script>  
  36. </body>  
  37. </html>  

index.css

CSS Code复制内容到剪贴板
  1. * {   
  2.     margin: 0;   
  3.     padding: 0;   
  4. }   
  5.   
  6. html, body {   
  7.     width: 100%;   
  8.     height: 100%;   
  9.     font-size14px;   
  10. }   
  11.   
  12. div.header {   
  13.     width: 100%;   
  14.     height100px;   
  15.     border-bottom1px dashed blue;   
  16. }   
  17.   
  18. div.title-outer {   
  19.     positionrelative;   
  20.     top: 50%;   
  21.     height30px;   
  22. }   
  23. span.title {   
  24.     text-alignleft;   
  25.     positionrelative;   
  26.     left: 3%;   
  27.     top: -50%;   
  28.     font-size22px;   
  29. }   
  30.   
  31. div.body {   
  32.     width: 100%;   
  33. }   
  34. .overlay {   
  35.     positionabsolute;   
  36.     top0px;   
  37.     left0px;   
  38.     z-index: 10001;   
  39.     display:none;   
  40.     filter:alpha(opacity=60);   
  41.     background-color#777;   
  42.     opacity: 0.5;   
  43.     -moz-opacity: 0.5;   
  44. }   
  45. .loading-tip {   
  46.     z-index: 10002;   
  47.     positionfixed;   
  48.     display:none;   
  49. }   
  50. .loading-tip img {   
  51.     width:100px;   
  52.     height:100px;   
  53. }   
  54.   
  55. .modal {   
  56.     position:absolute;   
  57.     width600px;   
  58.     height360px;   
  59.     border1px solid rgba(0, 0, 0, 0.2);   
  60.     box-shadow: 0px 3px 9px rgba(0, 0, 0, 0.5);   
  61.     displaynone;   
  62.     z-index: 10003;   
  63.     border-radius: 6px;   
  64. }   
  65.   

index.js

JavaScript Code复制内容到剪贴板
  1. function rightIFrameLoad(iframe) {   
  2.     var pHeight = getWindowInnerHeight() - $('#header').height() - 5;   
  3.        
  4.     $('div.body').height(pHeight);   
  5.     console.log(pHeight);   
  6.        
  7. }   
  8.   
  9. // 浏览器兼容 取得浏览器可视区高度   
  10. function getWindowInnerHeight() {   
  11.     var winHeight = window.innerHeight   
  12.             || (document.documentElement && document.documentElement.clientHeight)   
  13.             || (document.body && document.body.clientHeight);   
  14.     return winHeight;   
  15.        
  16. }   
  17.   
  18. // 浏览器兼容 取得浏览器可视区宽度   
  19. function getWindowInnerWidth() {   
  20.     var winWidth = window.innerWidth   
  21.             || (document.documentElement && document.documentElement.clientWidth)   
  22.             || (document.body && document.body.clientWidth);   
  23.     return winWidth;   
  24.        
  25. }   
  26.   
  27. /**  
  28.  * 显示遮罩层  
  29.  */  
  30. function showOverlay() {   
  31.     // 遮罩层宽高分别为页面内容的宽高   
  32.     $('.overlay').css({'height':$(document).height(),'width':$(document).width()});   
  33.     $('.overlay').show();   
  34. }   
  35.   
  36. /**  
  37.  * 显示Loading提示  
  38.  */  
  39. function showLoading() {   
  40.     // 先显示遮罩层   
  41.     showOverlay();   
  42.     // Loading提示窗口居中   
  43.     $("#loadingTip").css('top',   
  44.             (getWindowInnerHeight() - $("#loadingTip").height()) / 2 + 'px');   
  45.     $("#loadingTip").css('left',   
  46.             (getWindowInnerWidth() - $("#loadingTip").width()) / 2 + 'px');   
  47.                
  48.     $("#loadingTip").show();   
  49.     $(document).scroll(function() {   
  50.         return false;   
  51.     });   
  52. }   
  53.   
  54. /**  
  55.  * 隐藏Loading提示  
  56.  */  
  57. function hideLoading() {   
  58.     $('.overlay').hide();   
  59.     $("#loadingTip").hide();   
  60.     $(document).scroll(function() {   
  61.         return true;   
  62.     });   
  63. }   
  64.   
  65. /**  
  66.  * 模拟弹出模态窗口DIV  
  67.  * @param innerHtml 模态窗口HTML内容  
  68.  */  
  69. function showModal(innerHtml) {   
  70.     // 取得显示模拟模态窗口用DIV   
  71.     var dialog = $('#modalDiv');   
  72.        
  73.     // 设置内容   
  74.     dialog.html(innerHtml);   
  75.        
  76.     // 模态窗口DIV窗口居中   
  77.     dialog.css({   
  78.         'top' : (getWindowInnerHeight() - dialog.height()) / 2 + 'px',   
  79.         'left' : (getWindowInnerWidth() - dialog.width()) / 2 + 'px'  
  80.     });   
  81.        
  82.     // 窗口DIV圆角   
  83.     dialog.find('.modal-container').css('border-radius','6px');   
  84.        
  85.     // 模态窗口关闭按钮事件   
  86.     dialog.find('.btn-close').click(function(){   
  87.         closeModal();   
  88.     });   
  89.        
  90.     // 显示遮罩层   
  91.     showOverlay();   
  92.        
  93.     // 显示遮罩层   
  94.     dialog.show();   
  95. }   
  96.   
  97. /**  
  98.  * 模拟关闭模态窗口DIV  
  99.  */  
  100. function closeModal() {   
  101.     $('.overlay').hide();   
  102.     $('#modalDiv').hide();   
  103.     $('#modalDiv').html('');   
  104. }   

body.html

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>  
  2. <html lang="zh-CN">  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <meta http-equiv="X-UA-Commpatible" content="IE=edge">  
  6. <title>body 页面</title>  
  7. <style type="text/css">  
  8. * {   
  9.     margin: 0;   
  10.     padding: 0;   
  11. }   
  12.   
  13. html, body {   
  14.     width: 100%;   
  15.     height: 100%;   
  16. }   
  17.   
  18. .outer {   
  19.     width: 200px;   
  20.     height: 120px;   
  21.     position: relative;   
  22.     top: 50%;   
  23.     left: 50%;   
  24. }   
  25.   
  26. .inner {   
  27.     width: 200px;   
  28.     height: 120px;   
  29.     position: relative;   
  30.     top: -50%;   
  31.     left: -50%;   
  32. }   
  33.   
  34. .button {   
  35.     width: 200px;   
  36.     height: 40px;   
  37.     position: relative;   
  38. }   
  39.     
  40. .button#btnShowLoading {   
  41.     top: 0;   
  42. }   
  43.   
  44. .button#btnShowModal {   
  45.     top: 30%;   
  46. }   
  47.   
  48. </style>  
  49. <script type="text/javascript">  
  50.        
  51.     function showOverlay() {   
  52.         // 调用父窗口显示遮罩层和Loading提示   
  53.         window.top.window.showLoading();   
  54.   
  55.         // 使用定时器模拟关闭Loading提示   
  56.         setTimeout(function() {   
  57.             window.top.window.hideLoading();   
  58.         }, 3000);   
  59.   
  60.     }   
  61.   
  62.     function showModal() {   
  63.         // 调用父窗口方法模拟弹出模态窗口   
  64.         window.top.showModal($('#modalContent').html());   
  65.     }   
  66.        
  67. </script>  
  68. </head>  
  69. <body>  
  70.     <div class='outer'>  
  71.         <div class='inner'>  
  72.             <button id='btnShowLoading' class='button' onclick='showOverlay();'>点击弹出遮罩层</button>  
  73.             <button id='btnShowModal' class='button' onclick='showModal();'>点击弹出模态窗口</button>  
  74.         </div>  
  75.     </div>  
  76.        
  77.     <!-- 模态窗口内容DIV,将本页面DIV内容设置到父窗口DIV上并模态显示 -->  
  78.     <div id='modalContent' style='display: none;'>  
  79.         <div class='modal-container' style='width: 100%;height: 100%;background-color: white;'>  
  80.             <div style='width: 100%;height: 49px;position: relative;left: 50%;top: 50%;'>  
  81.                 <span style='font-size: 36px; width: 100%; text-align:center; display: inline-block; position:inherit; left: -50%;top: -50%;'>模态窗口1</span>  
  82.             </div>  
  83.             <button class='btn-close' style='width: 100px; height: 30px; position: absolute; right: 30px; bottom: 20px;'>关闭</button>  
  84.         </div>  
  85.     </div>  
  86.     <script type='text/javascript' src="js/jquery-1.10.2.js"></script>  
  87. </body>  
  88. </html>  
  89.   

运行结果:

初始化

显示遮罩层和Loading提示

显示遮罩层和模拟弹出模态窗口

以上就是本文的全部内容,希望对大家的学习有所帮助。

原文:http://www.cnblogs.com/haoqipeng/p/html-overlay.html

更多精彩内容其他人还在看

网页注释在IE中产生文字溢出

实验代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transiti
收藏 0 赞 0 分享

HTML教程:定义列表

原文:http://andymao.com/andy/post/104.html 上一节:有序列表 写完“无序列表”和“有序列表”之后已经有人和我说这两篇看得没什么意思。这两篇文章如果只以单向读取的形式阅读那么的确是没什
收藏 0 赞 0 分享

HTML教程:有序列表

原文:http://andymao.com/andy/post/103.html 上一节:无序列表 信息有时候是无序归纳的,有的却有着明确的顺序,在上一篇也提到了。那么简单的来想一下身边有哪些事物是有先后顺序的:操作步骤、排行榜、书目录……
收藏 0 赞 0 分享

HTML教程:无序列表

原文:http://andymao.com/andy/post/102.html 段落已经讲完了,那么一些基本的应用方式也讲了一些,那么是否已经应用了呢?当然应用可以更为丰富,那么这些就需要自己在实际工作中不断的摸索与思考,然后创新并总结得出新的应用形式。当然了段落不能当作
收藏 0 赞 0 分享

HTML网页制作的强大8条技巧

  虽然现在有许多网页制作工具能让您轻松地完成工作,但如果使用HTML则可以得到更大控制权,下面介绍几个小技巧。   1。使用<tt>,<i>,<br>语句来控制文字排版比用<pre>好得多。 如: <tt>实用
收藏 0 赞 0 分享

网页表格分割线去除方法

网页表格分割线去除方法。 其实上面的三个表格都有三行三列,隐藏分隔线的诀窍在于rules,察看这三个表格的源代码,我们可以看到<TABLE>标签中都有rules。它有三个参数(cols,rows,none),当rules=cols时,表格会隐藏纵向的分隔线,这
收藏 0 赞 0 分享

blockquote标记应用注意

关于语义化,不是一句两句就能说明白的,而且现在也没有一个官方的很严格的定义。关于<blockquote>没有争议的是: 1.引用一段较长的文字 2.可以使用cite标签或者属性 问题是<blockquote>引用的文字必须使用块级元素将他
收藏 0 赞 0 分享

网页表格表框制作技巧

网页表格表框制作技巧。 -------------------------------------------------------------------------------- 表格边框的显示与隐藏,是可以用frame参数来控制的。请注意它只控制表格的边框图,而不
收藏 0 赞 0 分享

HTML其实就是学习几个重要标记的应用

《这将是一场革命》一文出来以后。得到业界大伙的认同,当然与此同时也得到部分来自内部与外部的挑衅,不过的更加多的是更多人来寻问每一个点的细节。今晚回家很早就睡了,半夜在一个梦中醒来,梦里正在和小学的同学玩一个游戏——“The Next&rdquo
收藏 0 赞 0 分享

移动端专用的meta标签设置大全

不知道有没有人觉得,html的meta标签描述的头部信息特别多,下面这篇文章主要给大家分享介绍了关于移动端专用的meta设置的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多