「點擊按鈕,畫面中央浮現一個固定定位的視窗,背景鋪上半透明遮罩;瀏覽器尺寸改變時視窗仍舊保持水平垂直置中;點擊遮罩即可關閉」——這是登入框、收藏提示、行銷彈窗最常見的互動模式。以下範例完整實作它,並額外處理了 IE6 的相容問題。
把程式碼存成 html 檔即可執行,在 Firefox、IE6/7/8、Chrome 等環境下都能運作。核心知識點有三個:用 CSS 讓 div 水平垂直置中、用 JS 取得瀏覽器可視寬高、以及處理 resize 事件。
<!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=gb2312" /> <title>JS當前頁面登錄注冊框,固定DIV,底層陰影 - </title> <!-- 一個按鈕 點擊之後 顯示一個div div要水平 垂直居中 當我浏覽器高度 寬度變化的時候 div依然水平 垂直居中 點擊遮罩層,浮動層隱藏 知識點: 1.如何用css控制div水平垂直居中 2.如何用js取得浏覽器高度寬度 3.如何控制resize事件--> <style type="text/css"> .hidden{ display:none} /*評論浮動層*/ #smallLay{width:498px; height:100px;padding:4px 10px 10px;background-color:#FFFFFF;border:1px solid #05549d;color:#333333;line-height:24px;text-align:left;-webkit-box-shadow:5px 2px 6px #000;-moz-box-shadow:3px 3px 6px #555;} </style> </head> <script type="text/javascript"> //判斷浏覽器ie6創建的div的樣式和非ie6創建的div的樣式 //創建div function showid(idname) { var isIE = (document.all) ? true : false; var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6); var newbox = document.getElementById(idname); newbox.style.zIndex = "9999"; newbox.style.display = "block" newbox.style.position = !isIE6 ? "fixed" : "absolute"; newbox.style.top = newbox.style.left = "50%"; newbox.style.marginTop = -newbox.offsetHeight / 2 + "px"; newbox.style.marginLeft = -newbox.offsetWidth / 2 + "px"; var jihualayer = document.createElement("div"); jihualayer.id = "jihualayer"; jihualayer.style.width = jihualayer.style.height = "100%"; jihualayer.style.position = !isIE6 ? "fixed" : "absolute"; jihualayer.style.top = jihualayer.style.left = 0; jihualayer.style.backgroundColor = "#000"; jihualayer.style.zIndex = "9998"; jihualayer.style.opacity = "0.6"; document.body.appendChild(jihualayer); var sel = document.getElementsByTagName("select"); for (var i = 0; i < sel.length; i++) { sel[i].style.visibility = "hidden"; } function jihualayer_iestyle() { jihualayer.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px"; jihualayer.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px"; } function newbox_iestyle() { newbox.style.marginTop = document.documentElement.scrollTop - newbox.offsetHeight / 2 + "px"; newbox.style.marginLeft = document.documentElement.scrollLeft - newbox.offsetWidth / 2 + "px"; } if (isIE) { jihualayer.style.filter = "alpha(opacity=60)"; } if (isIE6) { jihualayer_iestyle() newbox_iestyle(); window.attachEvent("onscroll", function () { newbox_iestyle(); }) window.attachEvent("onresize", jihualayer_iestyle) } jihualayer.onclick = function () { newbox.style.display = "none"; jihualayer.style.display = "none"; for (var i = 0; i < sel.length; i++) { sel[i].style.visibility = "visible"; } } } </script> <body> <div style="width:200px; height:100px;"></div> <input name="" type="button" id="showbtn" value="點擊顯示" onclick="showid('smallLay')" /> <!--收藏浮層開始--> <div id="smallLay" style="display:none" > 這<a href="https://jihua.cnblogs.com" style="text-decoration:none; color:Gray;">是</a>懸浮層 </div> <!--收藏浮層結束--> <div id="a" style="height:1000px;">這<a href="https://jihua.cnblogs.com" style="text-decoration:none; color:Black;">是</a>普通層</div> </body> </html>
程式把視窗的 top 與 left 都設為 50%,讓左上角落在視窗正中央,再用負的 margin 把元素往左上推回自身尺寸的一半:
newbox.style.marginTop = -newbox.offsetHeight / 2 + "px"; newbox.style.marginLeft = -newbox.offsetWidth / 2 + "px";
這是 flexbox 普及之前最通用的寫法。今天若不必支援舊版 IE,直接用 display: flex; align-items: center; justify-content: center; 的容器,或讓視窗 position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);,都能少寫好幾行。
這裡沒有把遮罩寫在 HTML 裡,而是用 document.createElement("div") 在點擊時才建立並掛到 body,關閉時直接隱藏。好處是頁面初始狀態不殘留多餘節點,也方便同一頁多次開關。
IE6 不支援 position: fixed,所以程式偵測到 IE6 時改用 absolute,並額外掛上 scroll 與 resize 事件,隨時重算 margin 與遮罩尺寸來模擬固定定位的效果。同時用 filter: alpha(opacity=60) 補上 IE 系列的透明度支援。這些分支在今天已屬歷史包袱,但理解它們有助於維護老系統。
另外值得注意的是那段把 select 設為 visibility: hidden 的程式碼——這是為了避開舊 IE 中 select 會穿透遮罩層的著名 bug。關閉浮層時再還原,避免影響頁面其他操作。
浮層的立體感來自 box-shadow,並搭配 -webkit- 與 -moz- 前綴相容舊版瀏覽器。以現在的標準來說,只寫 box-shadow: 5px 2px 6px #000; 就足夠了。