js实现遮布效果

本文提供了一个用于在开发过程中实现提示对话框和页面跳转的通用方法,包括遮罩层和新弹出层的创建与关闭,适用于多种场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        开发过程常遇到在导入数据或者需要页面跳转的时,希望有一个提示效果。用的地方多了,就整理了一个,或许你也需要哦

var m = "mask";
var _id = "pop";

//给提示对话框设置大小
var	newDivHeight = 20;
	
function show(info) {
	openMaskDiv();
	openNewDiv(info);
}
	
//判断div是否已经创建
var docEle = function() {
   return document.getElementById(arguments[0]) || false;
 }
 
 //mask遮罩层             
function openMaskDiv() { 
  if (docEle(m)) 
     document.body.removeChild(docEle(m));
  var newMask = document.createElement("div");
  newMask.id = m;
  newMask.style.position = "absolute";
  newMask.style.zIndex = "1";
  //遮罩层的宽度和高度
	var _scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
  newMask.style.width = "100%";
  newMask.style.height = _scrollHeight + "px";
  newMask.style.top = "0px";
  newMask.style.left = "0px";
  newMask.style.background = "#AAAAAA";
  newMask.style.filter = "alpha(opacity=60)";
  newMask.style.opacity = "0.30";
  document.body.appendChild(newMask);
}  
          
//新弹出层
function openNewDiv(info) {
	if (docEle(_id)) 
	   document.body.removeChild(docEle(_id));
	var newDiv = document.createElement("div");
	newDiv.id = _id;
	newDiv.style.position = "absolute";
	newDiv.style.zIndex = "9999";
	newDiv.style.width = "auto";
	newDiv.style.height = newDivHeight + "px";
	newDiv.style.top = "50%";
        newDiv.style.left = "45%";
        newDiv.style.textAlign = "center";
	newDiv.style.background = "#EFEFEF";
	newDiv.style.border = "1px solid #AAA";
	newDiv.style.padding = "5px";
	newDiv.innerHTML = "<img src='images/loading.gif'/>&nbsp;"+info;
	document.body.appendChild(newDiv);
	
	//页面手动关闭
	//close();
	//可设置10秒后关闭提示
  setTimeout(auto_close,10000);
}

//关闭新图层和mask遮罩层
function close() {
   var newA = document.createElement("a");
	 newA.href = "#";
	 newA.innerHTML = "close";
	 newA.onclick = function() {
		    document.body.removeChild(docEle(_id));
		    document.body.removeChild(docEle(m));
		    return false;
	 }				
   newDiv.appendChild(newA);
}

function auto_close() {
	document.body.removeChild(docEle(_id));
	document.body.removeChild(docEle(m));
	return false;
}

 

<!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" />
    <script type="text/javascript" src="mask.js"  charset="utf-8"></script> 
</head>
<body>
    <input type="button" onclick="show('正在同步...');" value="pop"></button></br>
    this is test page;</br>
    this is test page;</br>
    this is test page;</br>
    this is test page;</br>
    this is test page;</br>
    this is test page;</br>
    this is test page;</br>
    this is test page;</br>
    </body>
</html>

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值