在HTML中实现和使用遮罩层
Web页面中使用遮罩层,可防止重复操作,提示loading;也可以模拟弹出模态窗口。
实现思路:一个DIV作为遮罩层,一个DIV显示loading动态GIF图片。在下面的示例代码中,同时展示了如何在iframe子页面中调用显示和隐藏遮罩层。
示例代码:
index.html
<!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="data:images/loading.gif" />
</div> <!-- 模拟模态窗口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>
css/index.css
* {
margin:;
padding:;
}
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:;
display:none;
filter:alpha(opacity=60);
background-color: #777;
opacity: 0.5;
-moz-opacity: 0.5;
}
.loading-tip {
z-index:;
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:;
border-radius: 6px;
}
js/index.js
function rightIFrameLoad(iframe) {
var pHeight = getWindowInnerHeight() - $('#header').height() - 5;
$('div.body').height(pHeight);
console.log(pHeight);
}
// 浏览器兼容 取得浏览器可视区高度
function getWindowInnerHeight() {
var winHeight = window.innerHeight
|| (document.documentElement && document.documentElement.clientHeight)
|| (document.body && document.body.clientHeight);
return winHeight;
}
// 浏览器兼容 取得浏览器可视区宽度
function getWindowInnerWidth() {
var winWidth = window.innerWidth
|| (document.documentElement && document.documentElement.clientWidth)
|| (document.body && document.body.clientWidth);
return winWidth;
}
/**
* 显示遮罩层
*/
function showOverlay() {
// 遮罩层宽高分别为页面内容的宽高
$('.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;
});
}
/**
* 模拟弹出模态窗口DIV
* @param innerHtml 模态窗口HTML内容
*/
function showModal(innerHtml) {
// 取得显示模拟模态窗口用DIV
var dialog = $('#modalDiv');
// 设置内容
dialog.html(innerHtml);
// 模态窗口DIV窗口居中
dialog.css({
'top' : (getWindowInnerHeight() - dialog.height()) / 2 + 'px',
'left' : (getWindowInnerWidth() - dialog.width()) / 2 + 'px'
});
// 窗口DIV圆角
dialog.find('.modal-container').css('border-radius','6px');
// 模态窗口关闭按钮事件
dialog.find('.btn-close').click(function(){
closeModal();
});
// 显示遮罩层
showOverlay();
// 显示遮罩层
dialog.show();
}
/**
* 模拟关闭模态窗口DIV
*/
function closeModal() {
$('.overlay').hide();
$('#modalDiv').hide();
$('#modalDiv').html('');
}
body.html
<!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() {
// 调用父窗口显示遮罩层和Loading提示
window.top.window.showLoading(); // 使用定时器模拟关闭Loading提示
setTimeout(function() {
window.top.window.hideLoading();
}, 3000); } function showModal() {
// 调用父窗口方法模拟弹出模态窗口
window.top.showModal($('#modalContent').html());
} </script>
</head>
<body>
<div class='outer'>
<div class='inner'>
<button id='btnShowLoading' class='button' onclick='showOverlay();'>点击弹出遮罩层</button>
<button id='btnShowModal' class='button' onclick='showModal();'>点击弹出模态窗口</button>
</div>
</div> <!-- 模态窗口内容DIV,将本页面DIV内容设置到父窗口DIV上并模态显示 -->
<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%;'>模态窗口1</span>
</div>
<button class='btn-close' style='width: 100px; height: 30px; position: absolute; right: 30px; bottom: 20px;'>关闭</button>
</div>
</div>
<script type='text/javascript' src="js/jquery-1.10.2.js"></script>
</body>
</html>
images/loading.gif
运行结果
初始化

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

END
在HTML中实现和使用遮罩层的更多相关文章
- htnl中的遮罩层以及定位方式
在页面显示遮罩层,例如:一个div的css样式: $msk.css({ "top":"0", "left":"0", & ...
- web页在微信中访问增加遮罩层 右上角弹出在浏览器中打开
https://blog.csdn.net/zgsdzczh/article/details/79744838 web页在微信中访问增加遮罩层 右上角弹出在浏览器中打开 <style typ ...
- AngualrJS中每次$http请求时的一个遮罩层Directive
在AngualrJS中使用$http每次向远程API发送请求,等待响应,这中间有些许的等待过程.如何优雅地处理这个等待过程呢? 如果我们在等待过程中弹出一个遮罩层,会是一个比较优雅的做法. 这就涉及到 ...
- 遮罩层中的相对定位与绝对定位(Ajax)
前提:公司最近做的一个项目列表,然后点击项目,出现背景遮罩层,弹出的数据框需要异步加载数据数据,让这个数据框居中,搞了两天终于总算达到Boss满意的程度,做了半年C/S,反过来做B/S,顿时感到技术还 ...
- easyui中使用的遮罩层
easyui 的 dialog 是继承自 window的,而 window中有modal这样的属性(见参考资料),就是用于打开模态的窗口的,也就是你说的有遮罩层的窗口.所以不需要额外的代码,仅需在di ...
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
史上最全的CSS hack方式一览 2013年09月28日 15:57:08 阅读数:175473 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我 ...
- Css动画形式弹出遮罩层,内容区上下左右居中于不定宽高的容器中
<!DOCTYPE html> <html> <head> </head> <body id="body"> <! ...
- 用JavaScript实现CheckBox的全选取消反选,及遮罩层中添加内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- html中设置透明遮罩层的兼容性代码
说明:下面遮罩层的height视实际情况自行修改,要求显示的div层的样式需加上position:relative,位于遮罩层层div的下面一行.<div id="ceng" ...
随机推荐
- windows10 自带笔记本键盘禁止和开启
管理员打开cmd,输入sc config i8042prt start= disabled 然后重启就好了,注意 =后面有个空格. 恢复:sc config i8042prt start= deman ...
- iOS8新特性(1)——UIAlertController
一.iOS8介绍 iOS8 新特性,主要是UI上进行了统一 1.UIAlertController 2.UIPresentaionController:管理所有通过modal出来的控制器(看笔记) 3 ...
- php---进行RSA进行非对称加密
参考文档: https://blog.csdn.net/zhihua_w/article/details/74002212 http://www.bm8.com.cn/webtool/rsa/http ...
- jQuery的下面是动态表格动态表单中的HTML代码
动态表格动态表单中的Jquery代码 <script type="text/javascript" src="/include/jquery/jquery-1.1. ...
- Saltstack生产案例之Haproxy安装
cd /srv/salt/prod/ mkdir haproxymkdir keepalivedmkdir nginxmkdir phpmkdir memcachedmkdir pkg cd pkg ...
- PAT天梯赛L2-004 这是二叉搜索树吗【递归】
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...
- cmake practice一文中安装可执行文件的方法
在学习cmake practice第四章中,第四章的任务如下 修改 Helloworld 支持安装在本节开头我们定义了本节的任务如下:1,为工程添加一个子目录 src,用来存储源代码;2,添加一个子目 ...
- 【转】仅此一文让你明白ASP.NET MVC原理
原文地址:http://www.cnblogs.com/DotCpp/p/3269043.html ASP.NET MVC由以下两个核心组成部分构成: 一个名为UrlRoutingModule的自定义 ...
- YARN架构设计详解
一.YARN基本服务组件 YARN是Hadoop 2.0中的资源管理系统,它的基本设计思想是将MRv1中的JobTracker拆分成了两个独立的服务:一个全局的资源管理器ResourceManager ...
- Oracle管理监控之oracle用户管理方法
创建用户语法: create user 用户名 identified by 密码: em:create user wangwc identified by tiger; 修改用户密码语法: alter ...