HTML實(shí)現(xiàn)遮罩層的方法 HTML中如何使用遮罩層
發(fā)布時(shí)間:2016-03-25 11:10:05 作者:MySomeDay
我要評論

這篇文章主要為大家詳細(xì)介紹了HTML實(shí)現(xiàn)遮罩層的方法,Web頁面中使用遮罩層,可防止重復(fù)操作,那么如何在HTML中使用遮罩層?感興趣的小伙伴們可以參考一下
Web頁面中使用遮罩層,可防止重復(fù)操作,提示loading;也可以模擬彈出模態(tài)窗口。
實(shí)現(xiàn)思路:一個(gè)DIV作為遮罩層,一個(gè)DIV顯示loading動(dòng)態(tài)GIF圖片。在下面的示例代碼中,同時(shí)展示了如何在iframe子頁面中調(diào)用顯示和隱藏遮罩層。
示例代碼:
index.html
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Commpatible" content="IE=edge">
- <title>HTML遮罩層</title>
- <link rel="stylesheet" href="css/index.css">
- </head>
- <body>
- <div class="header" id="header">
- <div class="title-outer">
- <span class="title">
- HTML遮罩層使用
- </span>
- </div>
- </div>
- <div class="body" id="body">
- <iframe id="iframeRight" name="iframeRight" width="100%" height="100%"
- scrolling="no" frameborder="0"
- style="border: 0px;margin: 0px; padding: 0px; width: 100%; height: 100%;overflow: hidden;"
- onload="rightIFrameLoad(this)" src="body.html"></iframe>
- </div>
- <!-- 遮罩層DIV -->
- <div id="overlay" class="overlay"></div>
- <!-- Loading提示 DIV -->
- <div id="loadingTip" class="loading-tip">
- <img src="images/loading.gif" />
- </div>
- <!-- 模擬模態(tài)窗口DIV -->
- <div class="modal" id="modalDiv"></div>
- <script type='text/javascript' src="js/jquery-1.10.2.js"></script>
- <script type="text/javascript" src="js/index.js"></script>
- </body>
- </html>
index.css
CSS Code復(fù)制內(nèi)容到剪貼板
- * {
- margin: 0;
- padding: 0;
- }
- html, body {
- width: 100%;
- height: 100%;
- font-size: 14px;
- }
- div.header {
- width: 100%;
- height: 100px;
- border-bottom: 1px dashed blue;
- }
- div.title-outer {
- position: relative;
- top: 50%;
- height: 30px;
- }
- span.title {
- text-align: left;
- position: relative;
- left: 3%;
- top: -50%;
- font-size: 22px;
- }
- div.body {
- width: 100%;
- }
- .overlay {
- position: absolute;
- top: 0px;
- left: 0px;
- z-index: 10001;
- display:none;
- filter:alpha(opacity=60);
- background-color: #777;
- opacity: 0.5;
- -moz-opacity: 0.5;
- }
- .loading-tip {
- z-index: 10002;
- position: fixed;
- display:none;
- }
- .loading-tip img {
- width:100px;
- height:100px;
- }
- .modal {
- position:absolute;
- width: 600px;
- height: 360px;
- border: 1px solid rgba(0, 0, 0, 0.2);
- box-shadow: 0px 3px 9px rgba(0, 0, 0, 0.5);
- display: none;
- z-index: 10003;
- border-radius: 6px;
- }
index.js
JavaScript Code復(fù)制內(nèi)容到剪貼板
- function rightIFrameLoad(iframe) {
- var pHeight = getWindowInnerHeight() - $('#header').height() - 5;
- $('div.body').height(pHeight);
- console.log(pHeight);
- }
- // 瀏覽器兼容 取得瀏覽器可視區(qū)高度
- function getWindowInnerHeight() {
- var winHeight = window.innerHeight
- || (document.documentElement && document.documentElement.clientHeight)
- || (document.body && document.body.clientHeight);
- return winHeight;
- }
- // 瀏覽器兼容 取得瀏覽器可視區(qū)寬度
- function getWindowInnerWidth() {
- var winWidth = window.innerWidth
- || (document.documentElement && document.documentElement.clientWidth)
- || (document.body && document.body.clientWidth);
- return winWidth;
- }
- /**
- * 顯示遮罩層
- */
- function showOverlay() {
- // 遮罩層寬高分別為頁面內(nèi)容的寬高
- $('.overlay').css({'height':$(document).height(),'width':$(document).width()});
- $('.overlay').show();
- }
- /**
- * 顯示Loading提示
- */
- function showLoading() {
- // 先顯示遮罩層
- showOverlay();
- // Loading提示窗口居中
- $("#loadingTip").css('top',
- (getWindowInnerHeight() - $("#loadingTip").height()) / 2 + 'px');
- $("#loadingTip").css('left',
- (getWindowInnerWidth() - $("#loadingTip").width()) / 2 + 'px');
- $("#loadingTip").show();
- $(document).scroll(function() {
- return false;
- });
- }
- /**
- * 隱藏Loading提示
- */
- function hideLoading() {
- $('.overlay').hide();
- $("#loadingTip").hide();
- $(document).scroll(function() {
- return true;
- });
- }
- /**
- * 模擬彈出模態(tài)窗口DIV
- * @param innerHtml 模態(tài)窗口HTML內(nèi)容
- */
- function showModal(innerHtml) {
- // 取得顯示模擬模態(tài)窗口用DIV
- var dialog = $('#modalDiv');
- // 設(shè)置內(nèi)容
- dialog.html(innerHtml);
- // 模態(tài)窗口DIV窗口居中
- dialog.css({
- 'top' : (getWindowInnerHeight() - dialog.height()) / 2 + 'px',
- 'left' : (getWindowInnerWidth() - dialog.width()) / 2 + 'px'
- });
- // 窗口DIV圓角
- dialog.find('.modal-container').css('border-radius','6px');
- // 模態(tài)窗口關(guān)閉按鈕事件
- dialog.find('.btn-close').click(function(){
- closeModal();
- });
- // 顯示遮罩層
- showOverlay();
- // 顯示遮罩層
- dialog.show();
- }
- /**
- * 模擬關(guān)閉模態(tài)窗口DIV
- */
- function closeModal() {
- $('.overlay').hide();
- $('#modalDiv').hide();
- $('#modalDiv').html('');
- }
body.html
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Commpatible" content="IE=edge">
- <title>body 頁面</title>
- <style type="text/css">
- * {
- margin: 0;
- padding: 0;
- }
- html, body {
- width: 100%;
- height: 100%;
- }
- .outer {
- width: 200px;
- height: 120px;
- position: relative;
- top: 50%;
- left: 50%;
- }
- .inner {
- width: 200px;
- height: 120px;
- position: relative;
- top: -50%;
- left: -50%;
- }
- .button {
- width: 200px;
- height: 40px;
- position: relative;
- }
- .button#btnShowLoading {
- top: 0;
- }
- .button#btnShowModal {
- top: 30%;
- }
- </style>
- <script type="text/javascript">
- function showOverlay() {
- // 調(diào)用父窗口顯示遮罩層和Loading提示
- window.top.window.showLoading();
- // 使用定時(shí)器模擬關(guān)閉Loading提示
- setTimeout(function() {
- window.top.window.hideLoading();
- }, 3000);
- }
- function showModal() {
- // 調(diào)用父窗口方法模擬彈出模態(tài)窗口
- window.top.showModal($('#modalContent').html());
- }
- </script>
- </head>
- <body>
- <div class='outer'>
- <div class='inner'>
- <button id='btnShowLoading' class='button' onclick='showOverlay();'>點(diǎn)擊彈出遮罩層</button>
- <button id='btnShowModal' class='button' onclick='showModal();'>點(diǎn)擊彈出模態(tài)窗口</button>
- </div>
- </div>
- <!-- 模態(tài)窗口內(nèi)容DIV,將本頁面DIV內(nèi)容設(shè)置到父窗口DIV上并模態(tài)顯示 -->
- <div id='modalContent' style='display: none;'>
- <div class='modal-container' style='width: 100%;height: 100%;background-color: white;'>
- <div style='width: 100%;height: 49px;position: relative;left: 50%;top: 50%;'>
- <span style='font-size: 36px; width: 100%; text-align:center; display: inline-block; position:inherit; left: -50%;top: -50%;'>模態(tài)窗口1</span>
- </div>
- <button class='btn-close' style='width: 100px; height: 30px; position: absolute; right: 30px; bottom: 20px;'>關(guān)閉</button>
- </div>
- </div>
- <script type='text/javascript' src="js/jquery-1.10.2.js"></script>
- </body>
- </html>
運(yùn)行結(jié)果:
初始化
顯示遮罩層和Loading提示
顯示遮罩層和模擬彈出模態(tài)窗口
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
原文:http://www.cnblogs.com/haoqipeng/p/html-overlay.html
相關(guān)文章
遮罩層 + Iframe實(shí)現(xiàn)界面自動(dòng)顯示的示例代碼
這篇文章主要介紹了遮罩層 + Iframe實(shí)現(xiàn)界面自動(dòng)顯示的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小2020-04-26- 彈出一個(gè)遮罩層有正在加載效果的文字,在很多場景都有看到過,下面為大家介紹下具體的實(shí)現(xiàn)2014-04-17
- 今天有任務(wù)讓加個(gè)蒙版,JS小白的我在網(wǎng)上找了個(gè)修改下,改成了JS模版以后可以重復(fù)用啦,本文將詳細(xì)介紹DIV遮罩層如何實(shí)現(xiàn),需要的 朋友可以參考下2012-11-20
div背景半透明,覆蓋整個(gè)可視區(qū)域的遮罩層效果
背景半透明,覆蓋整個(gè)可視區(qū)域的遮罩層效果在工作中經(jīng)常會(huì)遇到,這篇文章主要介紹了當(dāng)內(nèi)容超過一屏?xí)r如何做到多瀏覽器的兼容性。 下面我們通過一個(gè)簡單的例子看看如何實(shí)現(xiàn)2011-12-08div背景半透明 覆蓋整個(gè)可視區(qū)域的遮罩層效果
背景半透明,覆蓋整個(gè)可視區(qū)域的遮罩層效果在工作中經(jīng)常會(huì)遇到,這篇文章主要介紹了當(dāng)內(nèi)容超過一屏?xí)r如何做到多瀏覽器的兼容性。 下面我們通過一個(gè)簡單的例子看看如何實(shí)現(xiàn)2011-10-30Html5頁面點(diǎn)擊遮罩層背景關(guān)閉遮罩層
這篇文章主要介紹了Html5頁面點(diǎn)擊遮罩層背景關(guān)閉遮罩層,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起2020-11-30