HTML實作遮罩層的方法 HTML中如何使用遮罩層_HTML/Xhtml_網頁製作

WBOY
發布: 2016-05-16 16:45:59
原創
1625 人瀏覽過

Web頁面中使用遮罩層,可防止重複操作,提示loading;也可以模擬彈出模態視窗。

實現思路:一個DIV作為遮罩層,一個DIV顯示loading動態GIF圖。在下面的範例程式碼中,同時展示如何在iframe子頁面中呼叫顯示和隱藏遮罩層。

範例程式碼:

index.html

XML/HTML Code複製內容到剪貼簿
  1. html>  
  2. html lang=lang=lang
  3. =
  4. 裡> ">  
  5. >    字符集=
  6. 字符集=字符集>    http-equiv=http-equiv=
  7. =
  8. -UA-相容" 內容="IE=edge"> 🎜>
  9. 標題>HTML遮罩層標題>  
  10. 連結 rel=
  11. rel=rel  href
  12. ="css/index.css"> 🎜>
  13. >   身體>       div 
  14. ="標題"  id="標題">          
  15. div class="title-外層」>               
  16. 跨度
  17.  🎜>>                   HTML遮罩使用   
  18.             
  19. 跨度>  
  20.         
  21. div>  
  22.     
  23. div>          div class
  24. ="body"  id="body"> 🎜>> 
  25.         iframe p  名稱="iframeRight" 寬度🎜> 寬度🎜>寬度>「100%」 高度=「100%」              
  26. =「否>「0」               樣式=「邊框:0px;邊距:0px;溢位: 隱藏;"
  27.   
  28.             onload==
  29. "body.html"
  30. >iframe>       div>     
  31.             
  32.   
  33.     
  34. div 
  35. id
  36.  id id id id id id id id“疊> 🎜>
  37.  
  38. =“疊加”
  39. >
  40. div >              div 
  41. id
  42. ="加載提示"  類別="載入提示">          
  43. img
  44.  src  />
  45.   
  46.     div>
  47.                          div =
  48. "模態"  
  49. id="modalDiv">>          
  50.     腳本 類型 類型  src="js/jquery-1.10.2.js">> >>
  51. >
  52. > >> >> >腳本>       腳本 類型 類型 
  53.  src="js/index.js"><
  54. 腳本>  
身體

>  

html>   index.css CSS 代碼 將內容複製到剪切板
  1. * {   
  2.     邊距:0;  
  3.     填入:0;   
  4. }   
  5.   
  6. html, body {   
  7.     寬度:100%;   
  8.     :100%;  
  9.     字體大小14px;   14px
  10. ;   
  11. }   
  12.   
  13. div.header {        寬度
  14. :100%;        高度100px
  15. ;        下邊框1px  藍色;   
  16. }   
  17.   
  18. div.title-outer {   
  19.     職位相對;   相對
  20. ;        上方
  21. :50%;  
  22.     高度30px
  23. ;   
  24. 30px;   
  25. }   
  26. span.title {        文字對齊
  27. ;        職位相對
  28. ;   相對;        
  29. :3%;       
  30. 上方: -50%;       字體大小
  31. 22px;   
  32. }   
  33.   
  34. div.body {        
  35. 寬度
  36. :100%;   
  37. }   
  38. .overlay {        位置
  39. 絕對
  40. ;   絕對;    ;   
  41.     上方0px;  
  42.     
  43. 0px
  44. ;  
  45.     z 索引:10001;       
  46. 顯示:
  47. ;   ;        過濾器:alpha(不透明度= 60);   
  48.     
  49. 背景顏色
  50. #777;   #777;    ;        不透明度:0.5;        -moz-不透明度:0.5;    }  
  51. .loading-tip {   
  52.     z 索引:10002;  
  53.     位置固定;   固定
  54. ;        顯示:
  55. ;   
  56. ;   
  57. }   
  58. .loading-tip img {        寬度
  59. :
  60. 100px;       高度:
  61. 100px;  
  62. }   
  63.   
  64. .modal {        位置
  65. 絕對
  66. ;   絕對;    ;   
  67.     寬度600px;   
  68.     
  69. 高度360px;        邊框
  70. 1px  0, 0, 0.2);        盒子陰影:0px 3px
  71.  3px 0, 0, 0, 0.5);        顯示
  72. ;   
  73. ;   
  74.     z 索引:10003;  
  75.     
  76. 邊框
  77. -半徑:
  78. 6px
  79. ;   
}   

  
index.js JavaScript 程式碼 將內容複製到剪切板
  1. function rightIFrameLoad(iframe) {   
  2.     var pHeight = getWindowInnerHeight() - $('. ;           
  3.     $(
  4. 'div.body').height(pHeight);        console.log(pHeight);   
  5.        
  6. }   
  7.   
  8. // 瀏覽器相容 取得瀏覽器視覺區高度   
  9. function getWindowInnerHeight() {        
  10. var winHeight = window.innerHeight                || (document.documentElement && document.documentElement.clientHeight)   
  11.             || (document.body && document.body.clientHeight);   
  12.     
  13. return winHeight;           
  14. }   
  15.   
  16. // 瀏覽器相容 取得瀏覽器視覺區寬度   
  17. function getWindowInnerWidth() {        
  18. var winWidth = window.innerWidth                || (document.documentElement && document.documentElement.clientWidth)   
  19.             || (document.body && document.body.clientWidth);   
  20.     
  21. return winWidth;   
  22.        
  23. }   
  24.   
  25. /** 
  26.  * 顯示遮罩層  
  27.  */  
  28. function showOverlay() {   
  29.     
  30. // 遮罩層寬高分別為頁面內容的寬高   
  31.     $(
  32. '.overlay').css({'height':$()>. height(),'width':$(document).width()});   
  33.     $(
  34. '.overlay').show();   
  35. }  
  36.   
  37. /** 
  38.  * 顯示Loading提示  
  39.  */  
  40. function showLoading() {   
  41.     // 先顯示遮罩層   
  42.     showOverlay();   
  43.     // Loading提示視窗居中   
  44.     $("#loadingTip").css('top'
  45.             (getWindowInnerHeight() - $(
  46. "#loadingTip""#loadingTip");   
  47.     $(
  48. "#loadingTip").css('left'             (getWindowInnerWidth() - $(
  49. "#loadingTip"
  50. "#loadingTip""#loadingTip");   
  51.                
  52.     $("#loadingTip").show();   
  53.     $(document).scroll(function() {   
  54.         return false;     });   
  55. }   
  56.   
  57. /** 
  58.  * 隱藏Loading提示
  59.  
  60.  */
  61.   
  62. function hideLoading() {        $(
  63. '.overlay').hide();        $(
  64. "#loadingTip").hide();        $(document).scroll(
  65. function() {            
  66. return true true;
  67.     });   
  68. }  
  69.   
  70. /** 
  71.  * 模擬彈出模態視窗DIV  
  72.  * @param innerHtml 模態視窗HTML內容  
  73.  */  
  74. function showModal(innerHtml) {   
  75.     // 取得顯示模擬模態視窗使用DIV   
  76.     var dialog = $('#modalDiv''#modalDiv'
  77.        
  78.     // 設定內容   
  79.     dialog.html(innerHtml);   
  80.        
  81.     // 模態視窗DIV窗口居中   
  82.     dialog.css({   
  83.         'top' : (getWindowInnerHeight() - dialog.height()) : (getWindowInnerHeight() - . ,            
  84. 'left' : (getWindowInnerWidth() - dialog.wid()p. 🎜>     });           
  85.     
  86. // 窗口DIV圓角   
  87.     dialog.find('.modal-container').css(
  88. 'border-radius' '6px');                
  89. // 模態視窗關閉按鈕事件   
  90.     dialog.find('.btn-close').click(
  91. function).click(function >         closeModal();   
  92.     });   
  93.        
  94.     
  95. // 顯示遮罩層
  96.   
  97.     showOverlay();   
  98.        
  99.     
  100. // 顯示遮罩層
  101.   
  102.     dialog.show();   
  103. }   
  104.   
  105. /** 
  106.  * 模擬關閉模態視窗DIV  
  107.  */  
  108. function
  109.  closeModal() {   
  110.     $('.overlay'
  111. ).hide();   
  112.     $('#modalDiv'
  113. ).hide();   
  114.     $('#modalDiv'
  115. ).html(
  116. ''); }   

    body.html

    XML/HTML Code複製內容到剪貼簿
    1. html>  
    2. html lang=lang=lang
    3. =
    4. 裡> ">  
    5. >    字符集=
    6. 字符集=字符集>    http-equiv=http-equiv=
    7. =
    8. -UA-相容" 內容="IE=edge"> 🎜>
    9. 標題>正文頁面標題>  
    10. 樣式 
    11. 類型=
    12. "text/ css ">
    13.   
    14. * {   
    15.     邊距:0;   
    16.     填充:0;   
    17. }   
    18.   
    19. html, body {   
    20.     寬度:100%;   
    21.     高度:100%;   
    22. }   
    23.   
    24. .outer {   
    25.     寬度:200 像素;   
    26.     高度:120 像素;   
    27.     職位:親;   
    28.     上方:50%;   
    29.     左:50%;   
    30. }   
    31.   
    32. .inner {   
    33.     寬度:200 像素;   
    34.     高度:120 像素;   
    35.     職位:親屬;   
    36.     上方: -50%;   
    37.     左:-50%;   
    38. }   
    39.   
    40. .button {   
    41.     寬度:200 像素;   
    42.     高度:40 像素;   
    43.     職位:親;   
    44. }   
    45.     
    46. .button#btnShowLoading {   
    47.     上方:0;   
    48. }       .button#btnShowModal {        上方:30%;    }  
    49.   
    50. style>  
    51. script type=type=type=>
    52.   
    53.        
    54.     function showOverlay() {   
    55.         // 呼叫父視窗顯示遮罩層和Loading提示   
    56.         window.top.window.showLoading();   
    57.   
    58.         // 使用定時器模擬關閉Loading提示   
    59.         setTimeout(function() {   
    60.             window.top.window.hideLoading();   
    61.         }, 3000);   
    62.   
    63.     }   
    64.   
    65.     function showModal() {   
    66.         // 呼叫父視窗方法模擬彈出式模態視窗   
    67.         window.top.showModal($('#modalContent').html());        }  
    68.        
    69. script>  
    70. head>  
    71. body>  
    72.     div class='outer'>  
    73.         div class='inner'>  
    74.             button class='button' onclick> >'showOverlay();'>點選彈出遮罩層buttonbuttonbuttonbutton>   
    75.             
    76. buttonbuttonbutton class='button' onclick> >'showModal();'>點選彈出模態視窗buttonbuttonbutton
    77.            
    78. div
    79. >       
    80. div
    81. >
    82.   
    83.   
    84.                    div id='modalContent'
    85.  
    86. style='display: none;'>         
    87. div class='modal-container ' 
    88. style='width: 100%;height: 100%;background-color: white;'>               div>
    89.   
    90.                 ssss'字體-尺寸:36 像素; 寬度:100%; 文字對齊:居中; 顯示:內聯區塊; 職位:繼承; 左:-50%;上:-50%;'>模態視窗1跨度
    91. >               div
    92. >             按鈕 ' 樣式='寬度:100px; 高度:30 像素;  位置:絕對; 右:30 像素; '>關閉按鈕> > >
    93.         div>
    94.        div>
    95.           腳本 類型 類型 型 src="js/jquery-1.10.2.js"
    96. >> >> >>
    97. >> >> >腳本>
    98.   

    身體

    >

      

    html

    >

      

      

    運行結果: 初始化 顯示遮罩層和載入提示 顯示遮罩層和模擬彈出模態視窗 以上就是本文的全部內容,希望對大家的學習有所幫助。 譯:http://www.cnblogs.com/haoqipeng/p/html-overlay.html
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!