直接贴代码,一切从简,可塑性强。我不想做什么封装,因为大家用途各有不同,稍作加工就是你想要的,OK?
<!--弹出层,加遮罩,本脚本特点是在弹出层上可以继续弹出层并继续遮罩父节点div层。随着关闭最上层,也删除对应的遮罩。其中遮罩用到了滤镜效果,所以IE浏览器和其他需要分别实现。-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaScript弹出层</title>
<script type="text/javascript" src="script/lib/jquery.min.js"></script>
<style>
html,body{font-size:12px;margin:0px;height:100%;}
.mesWindow{border:#666 1px solid;background:#fff;}
</style>
<script>
var isIe=(document.all)?true:false;
var winArray = [];
//显示弹出窗����
function showMessageBox(content)
{
//初始化当前遮罩层
var back=document.createElement("div");
var styleStr="top:0px;left:0px;position:absolute;background:#666;width:100%;height:100%;";
styleStr+=(isIe)?"filter:alpha(opacity=0);":"opacity:0;";
back.style.cssText=styleStr;
document.body.appendChild(back);
showBackground(back,50);
winArray.push(back);
//初始化弹出窗
var mesW=document.createElement("div");
mesW.className="mesWindow";
mesW.innerHTML="<div><button οnclick='testMessageBox2(this)'>弹出</button><br><button οnclick='closeWindow(this)'>关闭</button>"+content+"</div>";
styleStr="left:200px;top:0;position:absolute;width:350px;height:500px;";
mesW.style.cssText=styleStr;
document.body.appendChild(mesW);
}
//显示遮罩层
function showBackground(obj,endInt)
{
if(isIe)
{
obj.filters.alpha.opacity+=1;
if(obj.filters.alpha.opacity<endInt)
{
setTimeout(function(){showBackground(obj,endInt)},5);
}
}else{
var al=parseFloat(obj.style.opacity);al+=0.01;
obj.style.opacity=al;
if(al<(endInt/100))
{setTimeout(function(){showBackground(obj,endInt)},5);}
}
}
//关闭弹出窗
function closeWindow(my)
{
$(my).parents(".mesWindow").remove();
$(winArray.pop()).remove(); //删除最顶层的遮罩
}
function testMessageBox()
{
messContent="测试内容1";
showMessageBox(messContent);
}
function testMessageBox2()
{
messContent="<div style='padding:20px 0 20px 0;text-align:center'>测试内容2</div>";
showMessageBox(messContent);
}
</script>
</head>
<body>
<div style="padding:20px">
<div style="text-align:center;"><a οnclick="testMessageBox(event);">点击打开弹出层</a></div>
</div>
</body>
</html>