基于jQuery的弹出框插件
更新时间:2012年03月18日 12:47:16 作者:
有时候在做项目时,会遇到点击按钮或者文字是,弹出一个对话框,为了方便,自己就动手写了一个这样的一个插件,方便以后使用。
html如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript" src="jquery-1.0.popwin.js"></script>
<script type="text/javascript">
$(function() {
$("#btn01").popwin({
element: "#box01",
title: "请填写以下您的基本信息"
});
$("#btn02").popwin({
element: "#box02",
title: "请登陆"
});
})
</script>
<title>DEMO</title>
</head>
<body>
<div id="box01">
<form action="" method="post" onsubmit="return check();">
姓名:
<input type="text" size="30" name="username" id="username" onblur="return check();" value="" /><span id="nameErr"></span>
<br />
<br />
密码:
<input type="password" size="30" name="password" onblur="return check();" id="password" value="" /><span id="passwordErr"></span>
<br />
<br />
邮箱:
<input type="text" size="30" id="email" value="" onblur="return check();" /><span id="emailErr"></span>
<br />
<br />
<input type="submit" value="确定" />
<input type="reset" value="取消" />
</form>
</div>
<div id="box02">
<form action="" method="post">
姓名:
<input type="text" size="30" value="" />
<br />
<br />
密码:
<input type="password" size="30" value="" />
<br />
<br />
<input type="submit" value="确定" />
<input type="reset" value="取消" />
</form>
</div>
<button value="注册" id="btn01">注册</button>
<button value="登陆" id="btn02">登陆</button>
</body>
</html>
js插件如下:
/*
* jquery.popwin.js 1.0
* Copyright (c) gaoyubao
* Date: 2012-01-12
* 1.点击按钮,可以弹出你想弹出的内容,只要设置一下id,或者class
2.浏览器窗口缩小的时候,弹出框始终是居中的
3.按ESC间,可以关闭窗口
*/
(function($) {
var css='<style type="text/css">* {margin: 0;padding: 0;}#bg{background-color: #000; position: absolute; left:0; top:0;opacity: 0.5;filter:alpha(opacity=50);} #flagBox {position: absolute;border: 1px solid #000;background-color: #fff;z-index:2000;}#titleBox {padding: 5px;background-color:#fc0; overflow:hidden;} #titleBox p {font-weight: bold;} #titleBox a {float: right;} #htmlCode {padding: 10px;} span {font-size: 12px; color: #f00; margin-left: 10px;}</style>';
$("head").append(css);
$.fn.popwin= function(options) {
var settings={
element: "element", //哪一个弹出框,可以是id,或者是class
width: 400,
height: 200,
title: "title" //弹出框的title
}
var s=$.extend(settings,options);
var htmlCode=$(s.element).html();
$(s.element).remove();
$.a={
//设置背景的宽和高
setBg: function() {
var bh=$("body").height(),wh=$(window).height(),ww=$(window).width();
if(bh>wh) {
wh=bh;
}
$("#bg").css({
width: ww,
height: wh
});
},
//设置弹出框居中
setFlag: function() {
var l=(document.documentElement.clientWidth-s.width)/2+"px",
t=(document.documentElement.clientHeight-s.height)/2+"px";
$("#flagBox").css({
width: s.width,
height: s.height,
left: l,
top: t
});
},
//弹出框关闭
setClose: function() {
$("#container").remove();
}
};
var html='<div id="container"><div id="bg"></div><div id="flagBox"><div id="titleBox"><a href="javascript:void(0)">close</a><p>'+s.title+'</p></div><div id="htmlCode">'+htmlCode+'</div></div></div>';
$(window).resize(function() {//调解窗口的大小
$.a.setFlag();
});
return this.each(function() {
$(this).bind("click",function(){
$("body").append(html);
$("#titleBox a").click(function() {
$.a.setClose();
});
$.a.setBg();
$.a.setFlag();
});
$(document).keydown(function(event) {
if(event.which=="27") {
$.a.setClose();
}
});
});
};
})(jQuery)
function isEmail(str) {
var reg = /^([a-zA-Z0-9_-])+@+([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])/;
if(reg.exec(str)) {
return false;
}else {
return true;
}
}
function check() {
var flag=true;
$("#nameErr").html('');
$("#passwordErr").html('');
$("#emailErr").html('');
var username=$("#username").val();
var password=$("#password").val();
var email=$("#email").val();
if(username=="" || username==null) {
$("#nameErr").html("姓名不能为空");
flag=false;
}
if(password=="") {
$("#passwordErr").html("密码不能为空");
flag=false;
}
if(email=="") {
$("#emailErr").html("邮箱不能为空");
flag=false;
}else if(isEmail(email)) {
$("#emailErr").html("邮箱格式错误");
flag=false;
}
return flag;
}
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript" src="jquery-1.0.popwin.js"></script>
<script type="text/javascript">
$(function() {
$("#btn01").popwin({
element: "#box01",
title: "请填写以下您的基本信息"
});
$("#btn02").popwin({
element: "#box02",
title: "请登陆"
});
})
</script>
<title>DEMO</title>
</head>
<body>
<div id="box01">
<form action="" method="post" onsubmit="return check();">
姓名:
<input type="text" size="30" name="username" id="username" onblur="return check();" value="" /><span id="nameErr"></span>
<br />
<br />
密码:
<input type="password" size="30" name="password" onblur="return check();" id="password" value="" /><span id="passwordErr"></span>
<br />
<br />
邮箱:
<input type="text" size="30" id="email" value="" onblur="return check();" /><span id="emailErr"></span>
<br />
<br />
<input type="submit" value="确定" />
<input type="reset" value="取消" />
</form>
</div>
<div id="box02">
<form action="" method="post">
姓名:
<input type="text" size="30" value="" />
<br />
<br />
密码:
<input type="password" size="30" value="" />
<br />
<br />
<input type="submit" value="确定" />
<input type="reset" value="取消" />
</form>
</div>
<button value="注册" id="btn01">注册</button>
<button value="登陆" id="btn02">登陆</button>
</body>
</html>
js插件如下:
复制代码 代码如下:
/*
* jquery.popwin.js 1.0
* Copyright (c) gaoyubao
* Date: 2012-01-12
* 1.点击按钮,可以弹出你想弹出的内容,只要设置一下id,或者class
2.浏览器窗口缩小的时候,弹出框始终是居中的
3.按ESC间,可以关闭窗口
*/
(function($) {
var css='<style type="text/css">* {margin: 0;padding: 0;}#bg{background-color: #000; position: absolute; left:0; top:0;opacity: 0.5;filter:alpha(opacity=50);} #flagBox {position: absolute;border: 1px solid #000;background-color: #fff;z-index:2000;}#titleBox {padding: 5px;background-color:#fc0; overflow:hidden;} #titleBox p {font-weight: bold;} #titleBox a {float: right;} #htmlCode {padding: 10px;} span {font-size: 12px; color: #f00; margin-left: 10px;}</style>';
$("head").append(css);
$.fn.popwin= function(options) {
var settings={
element: "element", //哪一个弹出框,可以是id,或者是class
width: 400,
height: 200,
title: "title" //弹出框的title
}
var s=$.extend(settings,options);
var htmlCode=$(s.element).html();
$(s.element).remove();
$.a={
//设置背景的宽和高
setBg: function() {
var bh=$("body").height(),wh=$(window).height(),ww=$(window).width();
if(bh>wh) {
wh=bh;
}
$("#bg").css({
width: ww,
height: wh
});
},
//设置弹出框居中
setFlag: function() {
var l=(document.documentElement.clientWidth-s.width)/2+"px",
t=(document.documentElement.clientHeight-s.height)/2+"px";
$("#flagBox").css({
width: s.width,
height: s.height,
left: l,
top: t
});
},
//弹出框关闭
setClose: function() {
$("#container").remove();
}
};
var html='<div id="container"><div id="bg"></div><div id="flagBox"><div id="titleBox"><a href="javascript:void(0)">close</a><p>'+s.title+'</p></div><div id="htmlCode">'+htmlCode+'</div></div></div>';
$(window).resize(function() {//调解窗口的大小
$.a.setFlag();
});
return this.each(function() {
$(this).bind("click",function(){
$("body").append(html);
$("#titleBox a").click(function() {
$.a.setClose();
});
$.a.setBg();
$.a.setFlag();
});
$(document).keydown(function(event) {
if(event.which=="27") {
$.a.setClose();
}
});
});
};
})(jQuery)
function isEmail(str) {
var reg = /^([a-zA-Z0-9_-])+@+([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])/;
if(reg.exec(str)) {
return false;
}else {
return true;
}
}
function check() {
var flag=true;
$("#nameErr").html('');
$("#passwordErr").html('');
$("#emailErr").html('');
var username=$("#username").val();
var password=$("#password").val();
var email=$("#email").val();
if(username=="" || username==null) {
$("#nameErr").html("姓名不能为空");
flag=false;
}
if(password=="") {
$("#passwordErr").html("密码不能为空");
flag=false;
}
if(email=="") {
$("#emailErr").html("邮箱不能为空");
flag=false;
}else if(isEmail(email)) {
$("#emailErr").html("邮箱格式错误");
flag=false;
}
return flag;
}
最新评论