DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 基於jQuery的彈出框插件之五
基於jQuery的彈出框插件之五
編輯:JQuery特效代碼     
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;
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved