基于JavaScript如何制作遮罩层对话框
1.遮罩层其实就是一个覆盖全界面的半透明的DIV,并处理zIndex使他浮于其他元素之上,是用户不能点击下边的元素,或者说点击没有反应。
2.在遮罩层上方在弹出一个层,由于遮罩层挡住了其他所有元素,用户只能点击弹出层,制造出模式窗口的假象。
废话不多说了,直接给大家贴js代码了。
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
<title>UntitledDocument</title>
<script>
functionopenDiv(newDivID)
{
varnewMaskID="mask";//遮罩层id
varnewMaskWidth=document.body.scrollWidth;//遮罩层宽度
varnewMaskHeight=document.body.scrollHeight;//遮罩层高度
//mask遮罩层
varnewMask=document.createElement("div");//创建遮罩层
newMask.id=newMaskID;//设置遮罩层id
newMask.style.position="absolute";//遮罩层位置
newMask.style.zIndex="1";//遮罩层zIndex
newMask.style.width=newMaskWidth+"px";//设置遮罩层宽度
newMask.style.height=newMaskHeight+"px";//设置遮罩层高度
newMask.style.top="0px";//设置遮罩层于上边距离
newMask.style.left="0px";//设置遮罩层左边距离
newMask.style.background="gray";//#33393C//遮罩层背景色
newMask.style.filter="alpha(opacity=40)";//遮罩层透明度IE
newMask.style.opacity="0.40";//遮罩层透明度FF
document.body.appendChild(newMask);//遮罩层添加到DOM中
window.open('http://www.baidu.com','_blank','width=500,height=260,menubar=no,toolbar=no');//弹出子页面,具体自用自改
//弹出层滚动居中
functionnewDivCenter()
{
newDiv.style.top=(document.body.scrollTop+document.body.clientHeight/2
-newDivHeight/2)+"px";
newDiv.style.left=(document.body.scrollLeft+document.body.clientWidth/2
-newDivWidth/2)+"px";
}
if(document.all)//处理滚动事件,使弹出层始终居中
{
window.attachEvent("onscroll",newDivCenter);
}
else
{
window.addEventListener('scroll',newDivCenter,false);
}
//关闭新图层和mask遮罩层
varnewA=document.createElement("span");
newA.href="#";
newA.style.position="absolute";//span位置
newA.style.left=350+"px";
newA.innerHTML="Close";
newA.onclick=function()//处理关闭事件
{
if(document.all)
{
window.detachEvent("onscroll",newDivCenter);
}
else
{
window.removeEventListener('scroll',newDivCenter,false);
}
document.body.removeChild(newMask);//移除遮罩层
document.body.removeChild(newDiv);////移除弹出框
returnfalse;
}
newDiv.appendChild(newA);//添加关闭span
}
</script>
</head>
<BODY>
<aonclick="openDiv('newDiv');"style="cursor:pointer">点我点我</a>
<br>
username:<inputtype="text"name="uname"/><br>
upwd:<inputtype="password"name="upwd"/>
<br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/>
<aonclick="openDiv('newDiv');"style="cursor:pointer">点我点我</a>
</BODY>
</html>
以上所述是小编给大家介绍的基于JavaScript如何制作遮罩层对话框的相关知识,希望对大家有所帮助。