DIV CSS 佈局教程網

用原生 JS 做出置中彈跳登入框與半透明遮罩
編輯:JavaScript技巧     

下面是一個可直接存成 html 執行的完整實例,做出一個點擊後在當前頁面置中浮出的登錄/註冊框,並附上半透明遮罩層,向下滾動時仍維持置中。已實測可相容 Firefox、IE6、IE7、IE8、Chrome。

要處理的三個知識點是:用 CSS 讓 div 水平垂直置中、用 JS 取得瀏覽器寬高、以及 resize/scroll 事件的處理。

<!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>
<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,因為 IE6 不支援 position:fixed
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);

  // 隱藏 select,避免它在舊版 IE 中穿透遮罩
  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%,再用負一半的 marginTop/marginLeft 把元素拉回來。因為要除以 2 的是元素自身的 offsetHeight/offsetWidth,所以必須在 display 設為 block 之後才讀取,順序不能顛倒。

IE6 的相容處理:IE6 不認 position:fixed,因此改用 absolute,並額外掛上 scroll 事件隨時重算 margin,讓浮層看起來仍固定於視窗中央;遮罩層則靠 resize 事件隨時把尺寸撐滿整個可捲動區域。

Select 穿透問題:舊版 IE 中的 select 是視窗級元件,會蓋在任何 z-index 之上。解法是在浮層顯示時把頁面上所有 select 設為 visibility:hidden,關閉時再還原。

點擊遮罩層即可關閉浮層,這也是使用者最習慣的收合方式。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved