做彈跳視窗時最麻煩的就是置中,尤其是還要顧到舊版 IE。以下這份寫法把各版本的差異都處理好了。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>彈出層置中</title>
<style type="text/css">
html,body { height:100%; margin:0px; font-size:12px; }
.mydiv {
background-color:#ff6;
border:1px solid #f90;
text-align:center;
line-height:40px;
font-size:12px;
font-weight:bold;
z-index:99;
width:300px; height:120px;
left:50%; top:50%; /* FF / IE7 */
margin-left:-150px !important; /* 本身寬度的一半 */
margin-top:-60px !important; /* 本身高度的一半 */
margin-top:0px;
position:fixed !important; /* FF / IE7 */
position:absolute; /* IE6 */
_top: expression(eval(document.compatMode &&
document.compatMode=='CSS1Compat') ?
documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2 :
document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2);
}
.bg {
background-color:#ccc;
width:100%; height:100%;
left:0; top:0;
filter:alpha(opacity=50); /* IE */
opacity:0.5; /* FF */
z-index:1;
position:fixed !important;
position:absolute;
_top: expression(eval(document.compatMode &&
document.compatMode=='CSS1Compat') ?
documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2 :
document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2);
}
</style>
<script type="text/javascript">
function showDiv(){
document.getElementById('popDiv').style.display='block';
document.getElementById('bg').style.display='block';
}
function closeDiv(){
document.getElementById('popDiv').style.display='none';
document.getElementById('bg').style.display='none';
}
</script>
</head>
<body>
<div id="popDiv" class="mydiv" style="display:none;">
標題<br/>內容<br/>
<a href="javascript:closeDiv()">關閉視窗</a>
</div>
<div id="bg" class="bg" style="display:none;"></div>
<div style="padding-top:20px;">
<input type="submit" name="" value="顯示層" onclick="javascript:showDiv()" />
</div>
</body>
</html>
經典的三步驟:left:50%; top:50% 先把左上角移到畫面中心,再用負的 margin 把元素往左上推回「自身尺寸的一半」,於是真正的中心就對齊了。這也是為什麼 margin-left 要寫 -150px(寬 300 的一半)、margin-top 要寫 -60px(高 120 的一半)。
<html>
<head><title></title>
<style>
.mesWindowTop {
font-size:12px; font-weight:bold; text-align:left;
}
.mesWindowContent { font-size:12px; }
.mesWindow {
background: none repeat scroll 0 0 #FFFFFF;
border:1px solid #666666;
}
</style>
</head>
<body>
<div id="mesWindow" class="mesWindow"
style="top:180px; position:absolute; width:600px;
margin-left:-300px; left:50%; z-index:9999;">
<div class="mesWindowTop">
<table width="100%" height="100%"><tbody><tr>
<td>視窗標題</td>
<td style="width:1px;">
<input type="button" value="關閉" class="close"
title="關閉視窗" onclick="closeWindow();">
</td>
</tr></tbody></table>
</div>
<div id="mesWindowContent" class="mesWindowContent">
<div style="padding:20px 0;text-align:center">訊息正文</div>
</div>
<div class="mesWindowBottom"></div>
</div>
</body>
</html>
上面那堆 expression hack 是為 IE5/IE6 準備的,現在完全不需要了。現代寫法:
.mydiv{
position: fixed;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
}
或者更乾脆,直接用 flex 讓父層置中:
.overlay{
position: fixed; inset: 0;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0,0,0,.5);
}
除了程式碼短很多,最大的好處是不必再手算尺寸——寬高變動時置中依然正確。另外記得為彈出層加上 role="dialog"、aria-modal="true",並處理 Esc 關閉與焦點鎖定,鍵盤使用者才不會掉進背景裡。