搜尋
首頁web前端html教學HTML實作遮罩層的方法 HTML中如何使用遮罩層_HTML/Xhtml_網頁製作

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
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
&gt; gt;的目的是什麼 元素?&gt; gt;的目的是什麼 元素?Mar 21, 2025 pm 12:34 PM

本文討論了HTML&lt; Progress&gt;元素,其目的,樣式和與&lt; meter&gt;元素。主要重點是使用&lt; progress&gt;為了完成任務和LT;儀表&gt;對於stati

&lt; datalist&gt;的目的是什麼。 元素?&lt; datalist&gt;的目的是什麼。 元素?Mar 21, 2025 pm 12:33 PM

本文討論了html&lt; datalist&gt;元素,通過提供自動完整建議,改善用戶體驗並減少錯誤來增強表格。Character計數:159

&lt; meter&gt;的目的是什麼。 元素?&lt; meter&gt;的目的是什麼。 元素?Mar 21, 2025 pm 12:35 PM

本文討論了HTML&lt; meter&gt;元素,用於在一個範圍內顯示標量或分數值及其在Web開發中的常見應用。它區分了&lt; meter&gt;從&lt; progress&gt;和前

視口元標籤是什麼?為什麼對響應式設計很重要?視口元標籤是什麼?為什麼對響應式設計很重要?Mar 20, 2025 pm 05:56 PM

本文討論了視口元標籤,這對於移動設備上的響應式Web設計至關重要。它解釋瞭如何正確使用確保最佳的內容縮放和用戶交互,而濫用可能會導致設計和可訪問性問題。

我如何使用html5&lt; time&gt; 元素以語義表示日期和時間?我如何使用html5&lt; time&gt; 元素以語義表示日期和時間?Mar 12, 2025 pm 04:05 PM

本文解釋了HTML5&lt; time&gt;語義日期/時間表示的元素。 它強調了DateTime屬性對機器可讀性(ISO 8601格式)的重要性,並在人類可讀文本旁邊,增強Accessibilit

如何使用HTML5表單驗證屬性來驗證用戶輸入?如何使用HTML5表單驗證屬性來驗證用戶輸入?Mar 17, 2025 pm 12:27 PM

本文討論了使用HTML5表單驗證屬性,例如必需的,圖案,最小,最大和長度限制,以直接在瀏覽器中驗證用戶輸入。

&lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼?&lt; iframe&gt;的目的是什麼。 標籤?使用時的安全考慮是什麼?Mar 20, 2025 pm 06:05 PM

本文討論了&lt; iframe&gt;將外部內容嵌入網頁,其常見用途,安全風險以及諸如對象標籤和API等替代方案的目的。

HTML5中跨瀏覽器兼容性的最佳實踐是什麼?HTML5中跨瀏覽器兼容性的最佳實踐是什麼?Mar 17, 2025 pm 12:20 PM

文章討論了確保HTML5跨瀏覽器兼容性的最佳實踐,重點是特徵檢測,進行性增強和測試方法。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具