點擊「註冊」後畫面變暗、中央彈出一個表單,這是表層互動最常見的樣式。本文用純原生 JavaScript 實作,不依賴任何程式庫。
<style>
* { margin: 0; padding: 0; font-size: 12px; }
html, body { height: 100%; width: 100%; }
#content { background: #FFFFFF; padding: 30px; height: 100%; }
#content a { font-size: 30px; color: #369; font-weight: 700; }
/* 彈出層本體 */
#alert {
border: 1px solid #369;
width: 300px; height: 150px;
background: #e2ecf5;
z-index: 1000;
position: absolute;
display: none;
}
#alert h4 { height: 20px; background: #369; color: #fff; padding: 5px 0 0 5px; }
#alert h4 span { float: left; }
#alert h4 span#close { margin-left: 210px; font-weight: 500; cursor: pointer; }
#alert p { padding: 12px 0 0 30px; }
#alert p input.myinp { border: 1px solid #ccc; height: 16px; }
#alert p input.sub { width: 60px; margin-left: 30px; }
/* 遮罩層 */
#mask {
position: absolute; top: 0; left: 0;
height: 100%; width: 100%;
background: #000; opacity: 0.3;
display: none; z-index: 1;
}
</style>
三層 z-index 的關係是重點:遮罩層 z-index:1 壓住頁面內容,彈出層 z-index:1000 壓在遮罩之上。兩者一開始都是 display:none。
<div id="content"> <a href="#">注冊</a> </div>
<div id="alert">
<h4><span>現在注冊</span><span id="close">關閉</span></h4>
<p>
<label> 用戶名</label>
<input type="text" class="myinp"
onmouseover="this.style.border='1px solid #f60'"
onfocus="this.style.border='1px solid #f60'"
onblur="this.style.border='1px solid #ccc'" />
</p>
<p>
<label> 密 碼</label>
<input type="password" class="myinp"
onmouseover="this.style.border='1px solid #f60'"
onfocus="this.style.border='1px solid #f60'"
onblur="this.style.border='1px solid #ccc'" />
</p>
<p>
<input type="submit" value="注冊" class="sub" />
<input type="reset" value="重置" class="sub" />
</p>
</div>
<div id="mask"></div><!-- 遮罩層 -->
var myAlert = document.getElementById("alert");
var myMask = document.getElementById('mask');
var reg = document.getElementById("content").getElementsByTagName("a")[0];
var mClose = document.getElementById("close");
reg.onclick = function() {
myMask.style.display = "block";
myAlert.style.display = "block";
// 用 50% + 負 margin 做水平垂直置中
myAlert.style.position = "absolute";
myAlert.style.top = "50%";
myAlert.style.left = "50%";
myAlert.style.marginTop = "-75px"; // 高度 150px 的一半
myAlert.style.marginLeft = "-150px"; // 寬度 300px 的一半
document.body.style.overflow = "hidden"; // 禁止背景捲動
}
mClose.onclick = function() {
myAlert.style.display = "none";
myMask.style.display = "none";
}
置中技巧:top:50%; left:50% 會讓元素的左上角落在畫面正中央,所以必須用負 margin 把自己往左上推回一半的寬高,才能真正置中。這裡的 -75px 和 -150px 就是硬寫出來的「一半」。
禁止背景捲動:document.body.style.overflow = "hidden" 是必要的一步。少了它,使用者在彈出層上滾動時,背景的頁面也會跟著動,視覺上很混亂。
opacity 0.3:讓遮罩半透明,既能突顯彈出層,又不會完全遮住背景,使用者還保有位置感。
onfoucs 應為 onfocus,我在上方程式碼中已修正。position:absolute 時,頁面捲動後彈出層會跑掉,應該改用 fixed。<label> 沒有 for 屬性,點擊文字不會聚焦到輸入框。用原生 <dialog> 元素,上述問題大部分自動解決:
<dialog id="regDialog">
<form method="dialog">
<h4>現在注冊 <button value="close" aria-label="關閉">✕</button></h4>
<p><label for="uname">用戶名</label>
<input id="uname" name="uname" type="text" required></p>
<p><label for="upass">密碼</label>
<input id="upass" name="upass" type="password" required></p>
<p><button type="submit" value="ok">注冊</button></p>
</form>
</dialog>
<style>
dialog::backdrop { background: rgba(0, 0, 0, .3); } /* 遮罩層 */
</style>
<script>
var dlg = document.getElementById("regDialog");
document.getElementById("openBtn").addEventListener("click", function () {
dlg.showModal(); // 自動處理焦點、Esc 關閉、背景凍結
});
</script>
showModal() 會自動完成:把焦點移入對話框、Tab 鍵只在框內循環、Esc 鍵關閉、背景元素從無障礙樹中排除、並在關閉後把焦點還給原本的觸發元素。::backdrop 則直接取代了那個手寫的遮罩 div。
若因為某些原因必須自己實作,那麼起碼要補上:焦點移入、焦點陷阱(focus trap)、Esc 監聽、點擊外部關閉、關閉後歸還焦點,以及彈出時給容器加上 role="dialog" aria-modal="true" aria-labelledby="..."。這些都是 <dialog> 已經幫你做好的事。