基于jQuery的彈出框插件
更新時(shí)間:2012年03月18日 12:47:16 作者:
有時(shí)候在做項(xiàng)目時(shí),會(huì)遇到點(diǎn)擊按鈕或者文字是,彈出一個(gè)對(duì)話框,為了方便,自己就動(dòng)手寫(xiě)了一個(gè)這樣的一個(gè)插件,方便以后使用。
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: "請(qǐng)?zhí)顚?xiě)以下您的基本信息"
});
$("#btn02").popwin({
element: "#box02",
title: "請(qǐng)登陸"
});
})
</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="注冊(cè)" id="btn01">注冊(cè)</button>
<button value="登陸" id="btn02">登陸</button>
</body>
</html>
js插件如下:
/*
* jquery.popwin.js 1.0
* Copyright (c) gaoyubao
* Date: 2012-01-12
* 1.點(diǎn)擊按鈕,可以彈出你想彈出的內(nèi)容,只要設(shè)置一下id,或者class
2.瀏覽器窗口縮小的時(shí)候,彈出框始終是居中的
3.按ESC間,可以關(guān)閉窗口
*/
(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", //哪一個(gè)彈出框,可以是id,或者是class
width: 400,
height: 200,
title: "title" //彈出框的title
}
var s=$.extend(settings,options);
var htmlCode=$(s.element).html();
$(s.element).remove();
$.a={
//設(shè)置背景的寬和高
setBg: function() {
var bh=$("body").height(),wh=$(window).height(),ww=$(window).width();
if(bh>wh) {
wh=bh;
}
$("#bg").css({
width: ww,
height: wh
});
},
//設(shè)置彈出框居中
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
});
},
//彈出框關(guān)閉
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() {//調(diào)解窗口的大小
$.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("郵箱格式錯(cuò)誤");
flag=false;
}
return flag;
}
復(fù)制代碼 代碼如下:
<!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: "請(qǐng)?zhí)顚?xiě)以下您的基本信息"
});
$("#btn02").popwin({
element: "#box02",
title: "請(qǐng)登陸"
});
})
</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="注冊(cè)" id="btn01">注冊(cè)</button>
<button value="登陸" id="btn02">登陸</button>
</body>
</html>
js插件如下:
復(fù)制代碼 代碼如下:
/*
* jquery.popwin.js 1.0
* Copyright (c) gaoyubao
* Date: 2012-01-12
* 1.點(diǎn)擊按鈕,可以彈出你想彈出的內(nèi)容,只要設(shè)置一下id,或者class
2.瀏覽器窗口縮小的時(shí)候,彈出框始終是居中的
3.按ESC間,可以關(guān)閉窗口
*/
(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", //哪一個(gè)彈出框,可以是id,或者是class
width: 400,
height: 200,
title: "title" //彈出框的title
}
var s=$.extend(settings,options);
var htmlCode=$(s.element).html();
$(s.element).remove();
$.a={
//設(shè)置背景的寬和高
setBg: function() {
var bh=$("body").height(),wh=$(window).height(),ww=$(window).width();
if(bh>wh) {
wh=bh;
}
$("#bg").css({
width: ww,
height: wh
});
},
//設(shè)置彈出框居中
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
});
},
//彈出框關(guān)閉
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() {//調(diào)解窗口的大小
$.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("郵箱格式錯(cuò)誤");
flag=false;
}
return flag;
}
您可能感興趣的文章:
- jQuery+css+html實(shí)現(xiàn)頁(yè)面遮罩彈出框
- jquery彈出框的用法示例(一)
- jquery實(shí)現(xiàn)一個(gè)簡(jiǎn)單好用的彈出框
- 基于jquery的彈出提示框始終處于窗口的居中位置(類似于alert彈出框的效果)
- jquery彈出框插件jquery.ui.dialog用法分析
- jQuery實(shí)現(xiàn)消息彈出框效果
- JQuery 彈出框定位實(shí)現(xiàn)方法
- js彈出框輕量級(jí)插件jquery.boxy使用介紹
- jquery.boxy彈出框(后隔N秒后自動(dòng)隱藏/自動(dòng)跳轉(zhuǎn))
- JQuery實(shí)現(xiàn)頁(yè)面彈出框
相關(guān)文章
jQuery實(shí)現(xiàn)彈窗居中效果類似alert()
本文給大家分享基于jquery實(shí)現(xiàn)彈窗居中效果類似于alert(),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-02-02完美解決jQuery 鼠標(biāo)快速滑過(guò)后,會(huì)執(zhí)行多次滑出的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決jQuery 鼠標(biāo)快速滑過(guò)后,會(huì)執(zhí)行多次滑出的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12跟著JQuery API學(xué)Jquery 之二 屬性
在選擇器的API學(xué)習(xí)中,走馬觀花的把選擇器過(guò)了一遍,但是選擇歸選擇,選擇出來(lái)了沒(méi)干什么事,也沒(méi)有什么用嘛2010-04-04jQuery使用contains過(guò)濾器實(shí)現(xiàn)精確匹配方法詳解
這篇文章主要介紹了jQuery使用contains過(guò)濾器實(shí)現(xiàn)精確匹配的方法,結(jié)合實(shí)例形式分析了contains過(guò)濾器的具體使用技巧,需要的朋友可以參考下2016-02-02IE8兼容Jquery.validate.js的問(wèn)題
本文主要介紹IE8兼容Jquery.validate.js兼容問(wèn)題并提供哦了解決方法。需要的朋友來(lái)看下吧2016-12-12jQuery實(shí)現(xiàn)拖動(dòng)效果的實(shí)例代碼
這篇文章給大家介紹了jquery實(shí)現(xiàn)拖動(dòng)效果的簡(jiǎn)單代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06