全屏透明遮罩层

  在网上转悠的时候,看到了一篇不错的效果,便转载过来,以免以后用的时候难得找。

  主要是运用"position:fixed;"属性。

  直接上代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>lock page</title>
<meta name="author" content="www.planeart.cn" />
<style>
#pageOverlay
{ position:fixed; top:0; left:0; z-index:1987; width:100%; height:100%; background:#000; filter:alpha(opacity=70); opacity:0.7; }
ul li
{ height:300px;}
#color1
{background:#6cf;}
#color2
{background:#09c;}
#color3
{background:#9ff;}
</style>
</head>
<body>
<ul id="test" onclick="document.getElementById('pageOverlay').style.visibility = 'visible'">
<li id="color1"></li>
<li id="color2"></li>
<li id="color3"></li>
</ul>
<div id="pageOverlay" onclick="this.style.visibility = 'hidden'"></div>
</body>
</html>

  可惜ie6并不支持"position:fixed;"属性,那么只有使用JS动态修改top值模拟静止定位,拖动滚动条的时候遮罩层肯定会抖动,因为每改变一次ie都会重新渲染一次。但是微软却给我们提供了一个非常有趣的特性,如果给html或者body标签设置一个静止定位的背景图片,层在拖动滚动条的时候就不会抖动了,几乎完美的模拟了“position:fixed”。

  上代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>lock page</title>
<meta name="author" content="www.planeart.cn" />
<style>
#pageOverlay
{ position:fixed; top:0; left:0; z-index:1987; width:100%; height:100%; background:#000; filter:alpha(opacity=70); opacity:0.7; }
/*IE6 fixed*/
* html
{ background:url(*) fixed; }
* html body
{ margin:0; height:100%; }
* html #pageOverlay
{ position: absolute; left: expression(documentElement.scrollLeft + documentElement.clientWidth - this.offsetWidth); top: expression(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight); }
ul li
{height:300px;}
#color1
{background:#6cf;}
#color2
{background:#09c;}
#color3
{background:#9ff;}
</style>
</head>
<body>
<ul id="test" onclick="document.getElementById('pageOverlay').style.visibility = 'visible'">
<li id="color1"></li>
<li id="color2"></li>
<li id="color3"></li>
</ul>
<div id="pageOverlay" onclick="this.style.visibility = 'hidden'"></div>
</body>
</html>

  现在在ie6及其他浏览器都能实现锁屏了,接着咱们就再添加一点脚本,让锁屏的时候后可以中断用户操作,把滚动条、滚轮、tab键、全选、刷新、右键统统禁止掉,模拟真正的‘锁屏’。

  上代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>lock page</title>
<meta name="author" content="www.planeart.cn" />
<style>
#pageOverlay
{ visibility:hidden; position:fixed; top:0; left:0; z-index:1987; width:100%; height:100%; background:#000; filter:alpha(opacity=70); opacity:0.7; }
/*IE6 fixed*/
* html
{ background:url(*) fixed; }
* html body
{ margin:0; height:100%; }
* html #pageOverlay
{ position: absolute; left: expression(documentElement.scrollLeft + documentElement.clientWidth - this.offsetWidth); top: expression(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight); }
*
{margin:0;padding:0;}
ul
{list-style:none;}
ul li
{height:300px;}
#color1
{background:#6cf;}
#color2
{background:#09c;}
#color3
{background:#9ff;}
</style>
<script>
(
function(){
// 获取对象
var $ = function (id){
return document.getElementById(id);
};
// 遍历
var each = function(a, b) {
for (var i = 0, len = a.length; i < len; i++) b(a[i], i);
};
// 事件绑定
var bind = function (obj, type, fn) {
if (obj.attachEvent) {
obj[
'e' + type + fn] = fn;
obj[type
+ fn] = function(){obj['e' + type + fn](window.event);}
obj.attachEvent(
'on' + type, obj[type + fn]);
}
else {
obj.addEventListener(type, fn,
false);
};
};

// 移除事件
var unbind = function (obj, type, fn) {
if (obj.detachEvent) {
try {
obj.detachEvent(
'on' + type, obj[type + fn]);
obj[type
+ fn] = null;
}
catch(_) {};
}
else {
obj.removeEventListener(type, fn,
false);
};
};

// 阻止浏览器默认行为
var stopDefault = function(e){
e.preventDefault
? e.preventDefault() : e.returnValue = false;
};
// 获取页面滚动条位置
var getPage = function(){
var dd = document.documentElement,
db
= document.body;
return {
left: Math.max(dd.scrollLeft, db.scrollLeft),
top: Math.max(dd.scrollTop, db.scrollTop)
};
};

// 锁屏
var lock = {
show:
function(){
$(
'pageOverlay').style.visibility = 'visible';
var p = getPage(),
left
= p.left,
top
= p.top;

// 页面鼠标操作限制
this.mouse = function(evt){
var e = evt || window.event;
stopDefault(e);
scroll(left, top);
};
each([
'DOMMouseScroll', 'mousewheel', 'scroll', 'contextmenu'], function(o, i) {
bind(document, o, lock.mouse);
});
// 屏蔽特定按键: F5, Ctrl + R, Ctrl + A, Tab, Up, Down
this.key = function(evt){
var e = evt || window.event,
key
= e.keyCode;
if((key == 116) || (e.ctrlKey && key == 82) || (e.ctrlKey && key == 65) || (key == 9) || (key == 38) || (key == 40)) {
try{
e.keyCode
= 0;
}
catch(_){};
stopDefault(e);
};
};
bind(document,
'keydown', lock.key);
},
close:
function(){
$(
'pageOverlay').style.visibility = 'hidden';
each([
'DOMMouseScroll', 'mousewheel', 'scroll', 'contextmenu'], function(o, i) {
unbind(document, o, lock.mouse);
});
unbind(document,
'keydown', lock.key);
}
};
bind(window,
'load', function(){
$(
'btn_lock').onclick = function(){
lock.show();
};
$(
'pageOverlay').onclick = function(){
lock.close();
};
});
})();
</script>
</head>
<body>
<button id="btn_lock" style="position: absolute; left:50%; top:40%; ">开启锁屏</button>
<ul id="test">
<li id="color1"></li>
<li id="color2"></li>
<li id="color3"></li>
</ul>
<div id="pageOverlay"></div>
</body>
</html>


  原文地址:http://www.planeart.cn/?p=792

原文地址:https://www.cnblogs.com/fu277/p/2312325.html