一、先看一下效果:
未弹出的效果:
弹出框效果
二、源代码:
html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>弹出层效果</title>
<style type="text/css">
*{margin:0;padding:0;}
ul{list-style:none;}
.waper {
margin: 0 auto;
width: 1200px;
}
#header {
background: none repeat scroll 0 0 #2a2c2e;
font-family: "微软雅黑";
height: 60px;
width:100%;
position:fixed;
z-index:502;
}
.loginbar{float:right;line-height:60px;}
.loginbar a{display:block;float:left;padding:0 20px;}
#header a, #footer a{text-decoration:none;color:#fff;}
.fixed{height:60px;clear:both;}
#main ul{margin:0;padding:0;clear:both;}
#main ul li{float:left;margin:15px 45px;width:300px;height:170px;border:1px solid #ccc;}
</style>
<link type="text/css" rel="stylesheet" href="pop.css"/>
</head>
<body>
<header id="header">
<div class="waper">
<div class="loginbar">
<a href="javascript:;" id="loginBtn">登录</a>
<a href="javascript:;" id="regBtn">注册</a>
</div>
</div>
</header>
<section class="main clearfix" id="main">
<div class="fixed"></div>
<div class="waper">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</section>
<footer id="footer">
</footer>
<script src="popup.js"></script>
<script>
var loginBtn = document.getElementById("loginBtn");
loginBtn.onclick = function(){
new Popup();
}
</script>
</body>
</html>
css文件
@charset "utf-8";
/* CSS Document */
*{margin:0;padding:0}
.popupbox{ position:fixed;z-index:9998;}
.popup{background:#fcfdfd;relative;height:100%;width:100%;position:absolute;background:#fff;z-index:10000;}
.popupborder{background:#000;opacity:0.6;filter:Alpha(opacity=60);position:absolute;border-radius:5px;top:-6px;left:-6px;z-index:9999;}
.popuptop{height:40px; line-height:40px; background:url(images/tcbg.gif) repeat-x; cursor:pointer;}
.popuptop span{font-size:14px; font-weight:bold; color:#fff;float:left; text-indent:20px;}
.popuptop a.popclose{display:block; background:url(images/close.png) no-repeat; width:22px; height:22px;float:right;margin-right:7px; margin-top:10px; cursor:pointer;}
.popuptop a.popclose:hover{background:url(images/close1.png) no-repeat;}
.popupinfo{padding-top:30px;height:95px;}
.popupbottom{ background-color: #f6f6f6;
border-top: 1px solid #dadee5;text-align:center;padding:10px 0;width:100%;position:absolute;bottom:0px;z-index:10000;}
.btn,.bluebtn{ background:#e6e6e6;
border: 1px solid #c4c4c4;
border-radius: 2px;
color: #333;
cursor: pointer;
display: inline-block;
font-family: inherit;
font-size: 100%;
line-height: normal;
margin: 0;
overflow: visible;
padding: 4px 10px;
text-align: center;
text-decoration: none;
text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
vertical-align: middle;
white-space: nowrap;}
.bluebtn{
background-color: #1b75b6;
background-position: 0 -120px;
border-color: #106bab #106bab #0d68a9;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
color:#fff;
}
.btn:hover,.bluebtn:hover{opacity:0.7;filter:Alpha(opacity=70);}
.mask{background:#000;opacity:0.5;filter:Alpha(opacity=50);position:absolute;z-index:9000;width:100%;height:100%;top:0;left:0;}
js文件
/*弹出层
* @author shixinke (http://www.shixinke.com)
*/
function Popup(options)
{
this.options = options;
this.init();
}
Popup.prototype = {
defaults:{//默认设置
title:'信息', //弹层标题
closeable:true,
width:300,
height:400,
content:'您好',
showBtn:false,
},
init:function(){//初始化
this.self = this;
this.options = this.extends(this.options, this.defaults);
this.create();
},
create:function(){//创建弹层html
var popObj = document.createElement("div");
var wrapperWidth = this.options.width+12;
var wrapperHeight = this.options.height+12;
var btnHtml = '';
if(this.options.showBtn)
{
btnHtml = '<div class="popupbottom">'+
'<input name="submit" type="button" class="bluebtn" value="确定" id="popupsubmit"/> '+
'<input name="cancel" type="button" class="btn" value="取消" id="popupcancel"/>'+
'</div>';
}
var html = '<div class="popup" style="width:'+this.options.width+'px;height:'+this.options.height+'px">'+
'<div class="popuptop"><span>'+this.options.title+'</span><a class="popclose" id="popclose"></a></div>'+
'<div class="popupinfo">'+
this.options.content+
'</div>'+
btnHtml+
'</div>'+
'<div class="popupborder" style="width:'+wrapperWidth+'px;height:'+wrapperHeight+'px"></div>';
popObj.className = 'popupbox';
popObj.id = 'popupbox';
popObj.style.width = wrapperWidth +'px';
popObj.style.height = wrapperHeight +'px';
popObj.innerHTML = html;
this.addMask();
document.body.appendChild(popObj);
var offset = this.getPosition();
popObj.style.left = offset[0]+"px";
popObj.style.top = offset[1]+"px";
this.events();
},
extends:function(obj1, obj2){//合并自定义属性和默认属性
var opts = obj2;
for(property in obj1)
{
if((obj1[property] != 'undefined') (obj1[property].length > 0))
{
opts[property] = obj1[property];
}
}
return opts;
},
events:function()
{
var closeBtn = document.getElementById("popclose");
closeBtn.onclick = this.close;
},
getPosition:function(){//获取位置
//
var clientWidth = document.documentElement.clientWidth || document.body.clientWidth;
var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
var offsetWidth = document.getElementById("popupbox").offsetWidth;
var offsetHeight = document.getElementById("popupbox").offsetHeight;
var x = (clientWidth-offsetWidth)/2;
var y = (clientHeight-offsetHeight)/2;
return [x,y];
},
addMask:function()
{
var maskObj = document.createElement("div");
var scrollWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
maskObj.className = 'mask';
maskObj.id = 'mask';
maskObj.style.width = scrollWidth+"px";
maskObj.style.height = scrollHeight+"px";
document.body.appendChild(maskObj);
},
close:function(){
var maskObj = document.getElementById("mask");
var popObj = document.getElementById("popupbox");
document.body.removeChild(maskObj);
document.body.removeChild(popObj);
},
}