欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

HTML实现遮罩层的方法 HTML中如何使用遮罩层_HTML/Xhtml_网页制作

程序员文章站 2022-04-13 09:13:52
...
Web页面中使用遮罩层,可防止重复操作,提示loading;也可以模拟弹出模态窗口。

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

示例代码:

index.html

XML/HTML Code复制内容到剪贴板
  1. /span>>
  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. div id="overlay" class="overlay">div>
  24. div id="loadingTip" class="loading-tip">
  25. img src="images/loading.gif" />
  26. div>
  27. div class="modal" id="modalDiv">div>
  28. script type='text/javascript' src="js/jquery-1.10.2.js">script>
  29. script type="text/javascript" src="js/index.js">script>
  30. body>
  31. html>

index.css

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

index.js

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

body.html

XML/HTML Code复制内容到剪贴板
  1. /span>>
  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. html, body {
  13. width: 100%;
  14. height: 100%;
  15. }
  16. .outer {
  17. width: 200px;
  18. height: 120px;
  19. position: relative;
  20. top: 50%;
  21. left: 50%;
  22. }
  23. .inner {
  24. width: 200px;
  25. height: 120px;
  26. position: relative;
  27. top: -50%;
  28. left: -50%;
  29. }
  30. .button {
  31. width: 200px;
  32. height: 40px;
  33. position: relative;
  34. }
  35. .button#btnShowLoading {
  36. top: 0;
  37. }
  38. .button#btnShowModal {
  39. top: 30%;
  40. }
  41. style>
  42. script type="text/javascript">
  43. function showOverlay() {
  44. // 调用父窗口显示遮罩层和Loading提示
  45. window.top.window.showLoading();
  46. // 使用定时器模拟关闭Loading提示
  47. setTimeout(function() {
  48. window.top.window.hideLoading();
  49. }, 3000);
  50. }
  51. function showModal() {
  52. // 调用父窗口方法模拟弹出模态窗口
  53. window.top.showModal($('#modalContent').html());
  54. }
  55. script>
  56. head>
  57. body>
  58. div class='outer'>
  59. div class='inner'>
  60. button id='btnShowLoading' class='button' onclick='showOverlay();'>点击弹出遮罩层button>
  61. button id='btnShowModal' class='button' onclick='showModal();'>点击弹出模态窗口button>
  62. div>
  63. div>
  64. div id='modalContent' style='display: none;'>
  65. div class='modal-container' style='width: 100%;height: 100%;background-color: white;'>
  66. div style='width: 100%;height: 49px;position: relative;left: 50%;top: 50%;'>
  67. span style='font-size: 36px; width: 100%; text-align:center; display: inline-block; position:inherit; left: -50%;top: -50%;'>模态窗口1span>
  68. div>
  69. button class='btn-close' style='width: 100px; height: 30px; position: absolute; right: 30px; bottom: 20px;'>关闭button>
  70. div>
  71. div>
  72. script type='text/javascript' src="js/jquery-1.10.2.js">script>
  73. body>
  74. html>

运行结果:

初始化

HTML实现遮罩层的方法 HTML中如何使用遮罩层_HTML/Xhtml_网页制作

显示遮罩层和Loading提示

HTML实现遮罩层的方法 HTML中如何使用遮罩层_HTML/Xhtml_网页制作

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

HTML实现遮罩层的方法 HTML中如何使用遮罩层_HTML/Xhtml_网页制作

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

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