This article describes the method of implementing the div pop-up layer by js. Share it for your reference. The specific analysis is as follows:
By the way, it is really easy to implement pop-up layers when various plug-ins are released. However, I sometimes think that those plug-ins are not practical and often find some pure JS original things. Let me share with you a native js div pop-up layer instance. Friends in need can take a look.
Needless to say, just post the code. There are codes and comments:
Copy the code as follows:/*
* Pop up the DIV layer
*/
function showDiv()
{
var Idiv = document.getElementById("Idiv");
var mou_head = document.getElementById('mou_head');
Idiv.style.display = "block";
//The following parts should be centered to display the pop-up layer
Idiv.style.left=(document.documentElement.clientWidth-Idiv.clientWidth)/2+document.documentElement.scrollLeft+"px";
Idiv.style.top =(document.documentElement.clientHeight-Idiv.clientHeight)/2+document.documentElement.scrollTop-50+"px";
//The following parts make the entire page gray-out-clickable
var procbg = document.createElement("div"); // Create a div first
procbg.setAttribute("id","mybg"); //Define the id of the div
procbg.style.background = "#000000";
procbg.style.width = "100%";
procbg.style.height = "100%";
procbg.style.position = "fixed";
procbg.style.top = "0";
procbg.style.left = "0";
procbg.style.zIndex = "500";
procbg.style.opacity = "0.6";
procbg.style.filter = "Alpha(opacity=70)";
//Add the background layer to the page
document.body.appendChild(procbg);
document.body.style.overflow = "hidden"; //Cancel scrollbar
//The following parts realize the drag effect of the pop-up layer
var posX;
var posY;
mou_head.onmousedown=function(e)
{
if(!e) e = window.event; //IE
posX = e.clientX - parseInt(Idiv.style.left);
posY = e.clientY - parseInt(Idiv.style.top);
document.onmousemove = mousemove;
}
document.onmouseup = function()
{
document.onmousemove = null;
}
function mousemove(ev)
{
if(ev==null) ev = window.event;//IE
Idiv.style.left = (ev.clientX - posX) + "px";
Idiv.style.top = (ev.clientY - posY) + "px";
}
}
function closeDiv() //Close the pop-up layer
{
var Idiv=document.getElementById("Idiv");
Idiv.style.display="none";
document.body.style.overflow = "auto"; //Restore page scrollbar
var body = document.getElementsByTagName("body");
var mybg = document.getElementById("mybg");
body[0].removeChild(mybg);
}
<!--Pop layer starts->
<div id="Idiv" style="display:none; position:absolute; z-index:1000; background:#67a3d9;">
<div id="mou_head"100px; height=10px;z-index:1001; position:absolute;">This is used to implement drag layers</div>
<input type="button" value="close" onclick="closeDiv();" />
</div>
<!--End->
As for some beautification effects, you can modify them yourself.
I hope this article will be helpful to everyone's JavaScript programming.