Jqueryがユーザー定義のポップアップを実現する例


プロジェクト開発において、javascriptが持っている提示ダイアログを使ったら、あまり綺麗ではないと思います。だから、普通は自分でいくつかのダイアログを定義しています。ここでボタンをクリックしたら、登録の窓を開けます。ウィンドウをクリックして、ドラッグ機能を実現できます。多すぎる文字のものはよく分かりません。直接コードで話します。ここでHTML、CSS、Jqueryコードをそれぞれ貼り付けました。HTML部分:
 
<button id="show" class="alter"> </button>
<!-- -->
<div class="box">
<div class="box_content">
<div class="title">
<h3> </h3>
<h2 id="close">×</h2>
</div>
<div class="content">
<table border="0" cellpadding="0" cellspacing="0">
<tr height="60px">
<td colspan="2">
<p class="prompt" id="username_p"> </p>
<input type="text" class="inputstyle ins" id="username"/>
</td>
</tr>
<tr height="60px">
<td colspan="2">
<p class="prompt" id="pwd_p"> </p>
<input type="password" class="inputstyle ins" id="pwd"/>
</td>
</tr>
<tr height="30px">
<td align="left"><input type="checkbox" checked="checked"/> </td>
<td align="right"><a href="#"> ?</a></td>
</tr>
<tr height="60px">
<td colspan="2"><input type="submit" value=" " class="inputstyle login" id="login"/></td>
</tr>
<tr height="30px">
<td colspan="2" align="right"><a href="#"> </a></td>
</tr>
</table>
</div>
<p style="width:100%;border-bottom:1px solid #EEEEEE"></p>
<div class="other">
<p> </p>
<ul>
<li>QQ</li>
<li>MSN</li>
<li></li>
</ul>
</div>
</div>
</div>
CSS部分コード:
 
<style type="text/css">
*{margin:0px;padding:0px;color:#555555;}
.alter{width:50px;height:30px;margin:10px}
.box{
width:100%;
height:100%;
position:fixed;
top:0;
left:0;
background: -moz-linear-gradient(rgba(11,11,11,0.5), rgba(11,11,11,0.1)) repeat-x rgba(11,11,11,0.1);
background:-webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(11,11,11,0.1)), to(rgba(11,11,11,0.1))) repeat-x rgba(11,11,11,0.1);
z-index:100000;
display:none;
}
.box_content{
height:420px;
width:350px;
background:white;
position:fixed;
top:0;
left:0;
}
.box_content .title{
height:45px;
width:100%;
background:#EEEEEE;
line-height:45px;
overflow:hidden;
}
.title:hover{cursor: move;}
.title h3{float:left;margin-left:20px;}
.title h2{float:right;margin-right:15px;color:#999999}
.title h2:hover{color:#444444;cursor:pointer}

.box_content .content,.other{margin:20px 20px 10px 20px;overflow:hidden;font:normal 14px " ";}
.content table{width:99%;}
.content .inputstyle,.prompt{height:35px;width:96.5%;padding-left:10px;}
.content .inputstyle{font:bold 18px/35px " ";}
.content a{
text-decoration: none;
color:#1B66C7
}
.content a:hover{text-decoration: underline;}
.content table .login{
height:45px;width:101%;
border:none;
background:#4490F7;
color:#FFFFFF;
font:bold 17px " ";
border-radius:4px;
}
.content table .login:hover{
background:#559BFC;
}
.content .prompt{
color:#999999;
position:absolute;
line-height:38px;
}

.box_content .other{font:normal 14px " ";}
.other ul{
list-style:none;
margin-top:15px;
}
.other ul li{
float:left;
height:30px;
width:30px;
margin-right:15px;
border-radius:20px;
background:#1B66C7;
color:white;
text-align:center;
line-height:30px
}
</style>
Jqueryコード:
 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
BoxInit.init();
});

var BoxInit={
wWidth:undefined,//
wHeight:undefined,//
show:undefined,//
box:undefined,//
boxContent:undefined,//
closeBox:undefined,//
loginBtn:undefined,//
init:function(){
var self=this;
//
self.show=$("#show");
self.box=$(".box");
self.boxContent=$(".box_content");
self.closeBox=$("#close");
self.loginBtn=$("#login");
//
self.wWidth=$(window).width();
self.wHeight=$(window).height();
//
self.show.click(function(){self.showBtn()});
//
self.closeBox.click(function(){self.closes()});
//
self.loginBtn.click(function(){self.login()});
//DIV
self.dragDrop();
//
self.controlPromptInfo();
},
/**
*
*/
showBtn:function(){
var self=this;
self.box.animate({"width":self.wWidth,"height":self.wHeight},function(){
//
self.boxContent.animate({
"left":(self.wWidth-self.boxContent.width())/2
},function(){
$(this).animate({"top":(self.wHeight-self.boxContent.height())/2});
});
});
},
/**
*
*/
closes:function(){
var self=this;
self.boxContent.animate({
"top":0
},function(){
$(this).animate({"left":-(self.wWidth-self.boxContent.width())/2},function(){
self.box.animate({"width":-self.wWidth,"height":-self.wHeight});
});
});
},
/**
*
*/
login:function(){
var self=this;
self.boxContent.animate({
"top":0
},function(){
$(this).animate({"left":-(self.wWidth-self.boxContent.width())/2},function(){
self.box.animate({"width":-self.wWidth,"height":-self.wHeight});
});

});
},
/**
*
*/
dragDrop:function(){
var self=this;
var move=false;//
var offsetX=0;// left
var offsetY=0;// top
var title=$(".title");
//
title.mousedown(function(){
move=true;// div move true
offsetX=event.offsetX;// Left offsetX
offsetY=event.offsetY;// Top offsetY
title.css({"cursor":"move"});
}).mouseup(function(){
// move hi false
move=false;
});
$(document).mousemove(function(){
if(!move){// move true,
return;
}
//move true
var x = event.clientX-offsetX; //event.clientX , offsetX X ( X)
var y = event.clientY-offsetY; //event.clientY , offsetX Y ( Y)
if(!(x<0||y<0||x>(self.wWidth-self.boxContent.width())||y>(self.wHeight-self.boxContent.height()))){
self.boxContent.css({"left":x,"top":y,"cursor":"move"});
}
});
},
/**
*
*/
controlPromptInfo:function(){
// ,
$("p[class*=prompt]").each(function(){
var pro=$(this);
pro.click(function(){
// ,
pro.hide().siblings("input").focus();
});
});
//
$("input[class*=ins]").each(function(){
var input=$(this);
//
input.blur(function(){
//
if(input.val()==""){
//
input.siblings(".prompt").show();
}
}).keyup(function(){//
if(input.val()==""){//
//
input.blur().siblings(".prompt").show();
}else{
//
input.siblings(".prompt").hide();
}
});
});
}
}
</script>
機能全体のコードはここにあります。