javascript实现一个简单的弹出窗
功能介绍:点击一个按钮,然后页面会弹出一个窗口,而页面原来的内容会保持不变,只是在其页面上加了一个遮罩层,设置了不透明度,弹出的窗口可设置在固定位置,也可以自由设定,常见于网站的登录按钮。
html页面:
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>js实现一个弹出框</title>
<styletype="text/css">
/*预先写好弹出窗的样式*/
#menu{height:900px;}
#close{
width:30px;
height:30px;
cursor:pointer;
position:absolute;
right:5px;
top:5px;
text-indent:-999em;
background-color:blue;
}
#mask{
background-color:pink;
opacity:0.5;
filter:alpha(opacity=50);
position:absolute;
left:0;
top:0;
z-index:1;
}
#login{
position:fixed;
z-index:2;
}
.loginCon{
position:relative;
width:670px;
height:380px;
/*background:url(img/loginBg.png)#2A2C2Ecentercenterno-repeat;*/
background-color:#ccc;
}
</style>
</head>
<body>
<divid="menu">
<divid="login-area">
<buttonid="btnLogin">登录</button>
</div>
</div>
</body>
</html>
js代码:
<script>
functionopenNew(){
//获取页面的高度和宽度
varsWidth=document.body.scrollWidth;
varsHeight=document.body.scrollHeight;
//获取页面的可视区域高度和宽度
varwHeight=document.documentElement.clientHeight;
varoMask=document.createElement("div");
oMask.id="mask";
oMask.style.height=sHeight+"px";
oMask.style.width=sWidth+"px";
document.body.appendChild(oMask);
varoLogin=document.createElement("div");
oLogin.id="login";
oLogin.innerHTML="<divclass='loginCon'><divid='close'>关闭</div></div>";
document.body.appendChild(oLogin);
//获取登陆框的宽和高
vardHeight=oLogin.offsetHeight;
vardWidth=oLogin.offsetWidth;
//设置登陆框的left和top
oLogin.style.left=sWidth/2-dWidth/2+"px";
oLogin.style.top=wHeight/2-dHeight/2+"px";
//点击关闭按钮
varoClose=document.getElementById("close");
//点击登陆框以外的区域也可以关闭登陆框
oClose.onclick=oMask.onclick=function(){
document.body.removeChild(oLogin);
document.body.removeChild(oMask);
};
};
window.onload=function(){
varoBtn=document.getElementById("btnLogin");
//点击登录按钮
oBtn.onclick=function(){
openNew();
returnfalse;
}
}
</script>
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练