在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" ...
随机推荐
- tomcat启动报错:serializer.jar (系统找不到指定的文件。)
下载最新对应版本的tomcat.移除之前的tomcat.删除原本全部tomcat的目录. 疑似tomcat的lib包被动过.
- 构建Maven项目时常见错误
一.Maven项目,右键-update project后JRE system Library变为JavaSE1.6 Dynamic Web Module 3.0 requires Java 1.6 o ...
- MatLab Mark Points 给点标序号
在MatLab中,我们有时要给画出的点按一定顺序标上序号,下面这个简单的例子实现这个功能: x=rand(,); y=rand(,); plot(x,y,'r*'); text(x+:));
- JSP学习_01
JSP cookie是存储在客户机的文本文件,保存了大量的轨迹信息.通常有三个步骤可以识别回头客:1)服务器脚本发送一系列cookie到浏览器,如姓名.年龄.ID等2)浏览器在本地存储这些信息以备不时 ...
- bin/hdfs dfs命令存在WARN util.NativeCodeLoader问题消除方法
例如:[hadoop@db01 hadoop-2.5.0]$ bin/hdfs dfs -ls17/03/01 21:50:33 WARN util.NativeCodeLoader: Unable ...
- 启用yarn日志聚集功能
在yarn-site.xml配置文件中添加如下内容: ##开启日志聚集功能 <property> <name>yarn.log-ag ...
- 学习计划 mysql desc表结构分析
在完成数据表建表后,我们需要知道我们的表结构是什么,有没有和构造表时差异的地方. -- 简单查看表结构 desc 表名 这里拿数据库的一张表中做示例 mysql> desc rental; +- ...
- CentOS7.2配置vsftpd
环境 CentOS7.2 (安装镜像CentOS-7-x86_64-DVD-1611) 本文默认使用root用户操作 目标 实现CentOS7.2上安装vsftpd,使用新创建的ftpuser用户便可 ...
- mvc debug无法进入controller
可能原因为,工程更改名称 进入工程bin目录下,删除所有文件即可
- Linux(CentOS)安装Mysql数据库
1.需要mysql-linux安装包 本次使用mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz进行安装 2.将此安装包上传至linux服务器 上传路径为:/usr ...