Home > Article > Web Front-end > How to create a mask layer dialog box based on JavaScript_javascript skills
1. The mask layer is actually a translucent DIV that covers the entire interface, and the zIndex is processed to make it float above other elements. The user cannot click on the underlying elements, or there is no response when clicking.
2. Pop up a layer above the mask layer. Since the mask layer blocks all other elements, the user can only click on the pop-up layer to create the illusion of a modal window.
No more nonsense, I will just post the js code for you.
<htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <title>UntitledDocument</title> <script> function openDiv(newDivID) { var newMaskID = "mask"; //遮罩层id var newMaskWidth =document.body.scrollWidth;//遮罩层宽度 var newMaskHeight =document.body.scrollHeight;//遮罩层高度 //mask遮罩层 var newMask = 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'); //弹出子页面,具体自用自改 //弹出层滚动居中 function newDivCenter() { 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遮罩层 var newA = 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);////移除弹出框 return false; } newDiv.appendChild(newA);//添加关闭span } </script> </head> <BODY> <a onclick="openDiv('newDiv');" style="cursor:pointer">点我点我</a> <br> username:<input type="text" name="uname"/><br> u p w d:<input type="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 /> <a onclick="openDiv('newDiv');" style="cursor:pointer">点我点我</a> </BODY> </html>
The above is the relevant knowledge that the editor introduces to you on how to create a mask layer dialog box based on JavaScript. I hope it will be helpful to everyone.