瀏覽器原生的 alert() 實用但醜,正式專案幾乎都會換成自訂對話框。以下實作一個點擊按鈕彈出的登入視窗,支援拖曳移動位置,並附帶提示文字自動隱藏的貼心細節。HTML、CSS、jQuery 三部分完整附上。
<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>
</ul>
</div>
</div>
</div>
* { 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: rgba(11, 11, 11, 0.5);
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 "Microsoft YaHei";
}
.content table { width: 99%; }
.content .inputstyle, .prompt { height: 35px; width: 96.5%; padding-left: 10px; }
.content .inputstyle { font: bold 18px/35px "Microsoft YaHei"; }
.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 "Microsoft YaHei";
border-radius: 4px;
}
.content table .login:hover { background: #559BFC; }
.content .prompt { color: #999999; position: absolute; line-height: 38px; }
.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;
}
<script type="text/javascript" src="https://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(); });
self.dragDrop(); // DIV 拖曳
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; // 游標距彈窗左側的偏移
var offsetY = 0; // 游標距彈窗頂部的偏移
var title = $(".title");
title.mousedown(function(){
move = true;
offsetX = event.offsetX;
offsetY = event.offsetY;
title.css({ "cursor": "move" });
}).mouseup(function(){
move = false;
});
$(document).mousemove(function(){
if(!move) return;
var x = event.clientX - offsetX;
var y = event.clientY - offsetY;
// 限制在視窗範圍內,避免拖出畫面
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>
BoxInit 物件,用 self = this 保留上下文,比一堆散落的全域函式好維護得多。event 是全域物件,建議改用事件參數 e 並搭配 e.pageX/pageY;另外登錄按鈕目前只是關閉視窗,實務上要接上表單驗證與 AJAX 請求。