直接貼代碼,一切從簡,可塑性強。我不想做什麼封裝,因爲大家用途各有不同,稍作加工就是你想要的,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>