纯js制作遮罩层对话框 -- g皓皓
2016-01-26 10:33
351 查看
//本文支持js在线工具测试、转载请注明出处。
<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>
出处:http://blog.csdn.net/xuexiaodong009/article/details/6605417
参考文档:http://blog.csdn.net/alex197963/article/details/4456174
相关文章推荐
- flexible.js 淘宝弹性布局方案
- 通过js控制时间,一秒一秒自己动
- JSON详解
- JS实现简单的二维矩阵乘积运算
- JSTL 核心标签库 使用(C标签)
- 前后台Json的转换
- jsp字段判空
- 探究Javascript模板引擎mustache.js使用方法
- JS创建函数:函数声明和函数表达式
- structs2 请求json数据
- javascript检测对象中是否存在某个属性
- Json转换利器Gson三、TypeAdapter的使用
- 一些常用的页面设置和JavaScript小技巧
- js实现密码强度
- JavaScript 运行机制详解:再谈Event Loop
- 深入理解JavaScript系列
- JS正则表达式比较常见用法
- eval解析JSON中的注意点
- JSON数据(语义格式)多对象解析
- JSP中 <c:if test="${key1!=null}">是什么意思