今日做了一个简单的DIV蒙层的demo
具体如下:
1、js代码
function createIframe(){
//mask遮罩层
var newMask=document.createElement("div");
newMask.id="mDiv";
newMask.style.position="absolute";
newMask.style.zIndex="1";
_scrollWidth=Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
_scrollHeight=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
newMask.style.width=_scrollWidth+"px";
newMask.style.height=_scrollHeight+"px";
newMask.style.top="0px";
newMask.style.left="0px";
newMask.style.background="#33393C";
newMask.style.filter="alpha(opacity=40)";
newMask.style.opacity="0.40";
newMask.style.display='none';
var objDiv=document.createElement("DIV");
objDiv.id="div1";
objDiv.name="div1";
objDiv.style.left=(_scrollWidth-480)/2+"px";
objDiv.style.top=(_scrollHeight-200)/2+"px";
objDiv.style.position="absolute";
objDiv.style.zIndex="2"; //加了这个语句让objDiv浮在newMask之上
objDiv.style.display="none"; //让objDiv预先隐藏
objDiv.style.border="solid #0033FF 0px;";
document.body.appendChild(newMask);
document.body.appendChild(objDiv);
var objDrag=document.getElementById("drag");
var drag=false;
var dragX=0;
var dragY=0;
objDrag.attachEvent("onmousedown",startDrag);
function startDrag(){
if(event.button==1&&event.srcElement.tagName.toUpperCase()=="DIV"){
objDrag.setCapture();
objDrag.style.background="#0000CC";
drag=true;
dragX=event.clientX;
dragY=event.clientY;
}
};
objDrag.attachEvent("onmousemove",Drag);
function Drag(){
if(drag){
var oldwin=objDrag.parentNode;
oldwin.style.left=oldwin.offsetLeft+event.clientX-dragX;
oldwin.style.top=oldwin.offsetTop+event.clientY-dragY;
oldwin.style.left=event.clientX-100;
oldwin.style.top=event.clientY-10;
dragX=event.clientX;
dragY=event.clientY;
}
};
objDrag.attachEvent("onmouseup",stopDrag);
function stopDrag(){
objDrag.style.background="#0033FF";
objDrag.releaseCapture();
drag=false;
};
}
function HideIframe(mDiv,objDiv){
mDiv.style.display='none';
objDiv.style.display = "none"; //隐藏浮动的div
}
function htmlEditor(json){
var objDiv=document.getElementById("div1");
var mDiv=document.getElementById("mDiv");
mDiv.style.display='';
objDiv.innerHTML=json;
objDiv.style.display = ""; //显示浮动的div
}
html代码:
function add(){
$.get("${contextPath}base/citys.do",
{"action":"preAdd"},
function(json){
htmlEditor(json);
}
); }
<body οnlοad="createIframe();"></body>