Home  >  Article  >  Web Front-end  >  How to implement mask layer in HTML How to use mask layer in HTML_HTML/Xhtml_Web page production

How to implement mask layer in HTML How to use mask layer in HTML_HTML/Xhtml_Web page production

WBOY
WBOYOriginal
2016-05-16 16:45:591625browse

Using a mask layer in Web pages can prevent repeated operations and prompt loading; it can also simulate pop-up modal windows.

Implementation idea: One DIV serves as the mask layer, and one DIV displays the loading dynamic GIF image. In the following sample code, it also shows how to call the display and hiding mask layer in the iframe subpage.

Sample code:

index.html

XML/HTML CodeCopy content to clipboard
  1. >  
  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.       
  25.     <div id="overlay" class="overlay">div>  
  26.       
  27.     <div id="loadingTip" class="loading-tip">  
  28.         <img src="images/loading.gif" />  
  29.     div>  
  30.        
  31.       
  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. // Browser Compatibility Get browser viewport height
  9. function getWindowInnerHeight() {
  10. var winHeight = window.innerHeight
  11. || (document.documentElement && document.documentElement.clientHeight)
  12. || (document.body && document.body.clientHeight);
  13. return winHeight;
  14.  
  15. }
  16. // Browser compatibility Get browser viewport width
  17. function getWindowInnerWidth() {
  18. var winWidth = window.innerWidth
  19. || (document.documentElement && document.documentElement.clientWidth)
  20. || (document.body && document.body.clientWidth);
  21. return winWidth;
  22.  
  23. }
  24. /**
  25. * Show mask layer
  26. */
  27. function showOverlay() {
  28. //The width and height of the mask layer are the width and height of the page content respectively
  29. $('.overlay').css({'height':$(document). height(),'width':$(document).width()});
  30. $('.overlay').show();
  31. }
  32. /**
  33. * Show Loading prompt
  34. */
  35. function showLoading() {
  36. //Display the mask layer first
  37. showOverlay();
  38. // Loading prompt window is centered
  39. $("#loadingTip").css('top',
  40. (getWindowInnerHeight() - $("#loadingTip").height()) / 2 'px');
  41. $("#loadingTip").css('left',
  42. (getWindowInnerWidth() - $("#loadingTip").width()) / 2 'px');
  43.                                                   
  44. $(
  45. "#loadingTip").show();
  46. $(document).scroll(
  47. function() {
  48. return false;
  49. });
  50. }
  51. /**
  52. * Hide Loading prompt
  53. */
  54. function hideLoading() {
  55. $(
  56. '.overlay').hide();
  57. $(
  58. "#loadingTip").hide();
  59. $(document).scroll(
  60. function() {
  61. return true;
  62. });
  63. }
  64. /**
  65. * Simulate pop-up modal window DIV
  66. * @param innerHtml modal window HTML content
  67. */
  68. function showModal(innerHtml) {
  69. // Get the DIV used to display the simulation modal window
  70. var dialog = $('#modalDiv');
  71.  
  72. // Setting content
  73. dialog.html(innerHtml);
  74.  
  75. // Modal window DIV window is centered
  76. dialog.css({
  77.  'top' : (getWindowInnerHeight() - dialog.height()) / 2 'px' ,
  78.  'left' : (getWindowInnerWidth() - dialog.width()) / 2 'px'
  79. });
  80.  
  81. // Window DIV rounded corners
  82. Dialog.find('.modal-container').css('border-radius', '6px');
  83.  
  84. // Modal window close button event
  85. dialog.find('.btn-close').click(function(){
  86. closeModal();
  87. });
  88.  
  89. //Display mask layer
  90. showOverlay();
  91.  
  92. //Display mask layer
  93. dialog.show();
  94. }
  95. /**
  96. * Simulate closing modal window DIV
  97. */
  98. function closeModal() {
  99. $('.overlay').hide();
  100. $('#modalDiv').hide();
  101. $('#modalDiv').html('');
  102. }

body.html

XML/HTML CodeCopy content to clipboard
  1. >  
  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. style>
  48. <script type="text/ javascript">
  49.  
  50. function showOverlay() {
  51. // Call the parent window to display the mask layer and Loading prompts
  52. window.top.window.showLoading();
  53. // Use a timer to simulate closing the Loading prompt
  54. setTimeout(function() {
  55. window.top.window.hideLoading();
  56. }, 3000);
  57. }
  58. function showModal() {
  59. // Call the parent window method to simulate a pop-up modal window
  60. window.top.showModal($('#modalContent').html());
  61. }
  62.  
  63. script>
  64. head>
  65. <body>
  66.  <div class='outer' >
  67.  <div class='inner' >
  68.  <button id='btnShowLoading' class='button' onclick='showOverlay();'>Click to pop up the mask layerbutton>
  69.  <button id='btnShowModal' class='button' onclick='showModal();'>Click to pop up the modal windowbutton>
  70. div>
  71.  div> 
  72.  
  73.  <div id='modalContent' style='display: none;'>
  74.  <div class='modal- container' style='width: 100%;height: 100%;background-color: white;'>
  75.  <div style='width: 100%;height: 49px;position: relative;left: 50%;top: 50%;'>
  76.                 <span style='font-size: 36px; width: 100%; text-align:center; display: inline-block; position:inherit; left: -50%;top: -50%;'>模态窗口1span>  
  77.             div>  
  78.             <button class='btn-close' style='width: 100px; height: 30px; position: absolute; right: 30px; bottom: 20px;'>关闭button>  
  79.         div>  
  80.     div>  
  81.     <script type='text/javascript' src="js/jquery-1.10.2.js">script>  
  82. body>  
  83. html>  
  84.   

运行结果:

初始化

显示遮罩层和Loading提示

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

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn