DIV CSS 佈局教程網

用 jQuery 做置中遮罩層的完整示範
編輯:JavaScript綜合知識     

遮罩層(modal)的組成只有兩件事:一個蓋住全頁的半透明層,以及一個畫面正中央的對話框。示範把「樣板」寫在頁面裡隱藏起來,需要時複製一份掛上 body,關閉時移除,這樣就能支援多層堆疊。

頁面結構

<div class="left-container layout-container">
  <ul class="topic-title">
    <li><span class="fun">++++</span></li>
  </ul>
</div>
<div class="right-container layout-container"></div>

<!-- 對話框樣板,平時隱藏 -->
<div style="display:none">
  <div class="topic-input-div" id="content-container-templet">
    <ul class="topic-input-ul">
      <li class="input-li">
        <input type="text" style="width:100%;line-height:2em" placeholder="請輸入標題" />
      </li>
      <li class="btn-li"><button class="sure">確定</button></li>
      <li class="btn-li"><button class="cancel">取消</button></li>
    </ul>
  </div>
</div>

<script>
$(document).ready(function () {
  $('.fun').on('click', function () {
    showAddTopic(function (val) { console.log(val); });
  });
});
</script>

shadow.js:開與關

var shadow_list = [];

function showAddTopic(cb) {
  var input_container = $('#content-container-templet').clone();
  input_container.removeAttr('id');                 // 避免 id 重複

  var shadow = $('<div />').addClass('shadow-wrapper').append(input_container);
  shadow.appendTo(document.body);
  shadow_list.push(shadow);                          // 記住順序,才能正確關閉

  $('.sure', shadow).on('click', function () {
    cb($('.input-li input', shadow).val());          // 回呼把值交出去
    hideAddTopic();
  });
  $('.cancel', shadow).on('click', function () {
    hideAddTopic();
  });
}

function hideAddTopic() {
  if (shadow_list.length > 0) {
    shadow_list.pop().remove();                      // 後進先出
  }
}

兩個設計值得學。一是用 clone() 複製樣板並 removeAttr('id'),否則同一頁會出現兩個相同 id,getElementById 只會取到第一個;二是用陣列當堆疊,pop() 保證關掉的是最上層那一個,這種「後進先出」正是多層 modal 該有的行為。事件綁定時帶上 shadow 作為 context,能確保 $('.sure') 只選到目前這層的按鈕,不會誤觸底層。

CSS:置中的算法

html, body { padding:0; margin:0; width:100%; height:100%; overflow:hidden; }
ul, li { padding:0; margin:0; list-style:none; }

.layout-container { position:absolute; top:0; height:100%; overflow:auto; }
.left-container  { left:0;  width:20%; border:solid 5px #52BE7F; }
.right-container { right:0; width:80%; }

.shadow-wrapper { position:absolute; width:100%; height:100%; }

.topic-input-div {
  position: absolute;
  left: 50%; top: 50%;
  height: 200px; width: 400px;
  margin-top: -120px;      /* 高度(含 padding)的一半再微調 */
  margin-left: -210px;     /* 寬度的一半 */
  padding: 20px 10px;
}
.topic-input-ul {
  position:absolute; width:380px; height:2em;
  left:50%; top:50%; margin-top:-1em; margin-left:-190px;
}
.topic-input-ul li { line-height:2em; display:inline-block; *display:inline; }
.topic-input-ul .input-li { width:40%; }
.topic-input-ul .btn-li   { width:15%; }

置中的原理是 left/top 50% 把左上角推到中心,再用負的 margin 把自己拉回一半的距離。要注意 margin 值必須含 padding 一起算,範例的 -120 對 200 + 40(padding) 其實略為偏上,若要精準可改用 transform: translate(-50%, -50%) 搭配 top:50%;left:50%,不必再手算像素。

還缺少的部分

這份示範專注在 DOM 操作,遮罩層本身沒有背景色與 z-index,實際上會看不見「被遮住」的視覺效果,請在 .shadow-wrapper 補上 background:#000;opacity:.5;z-index:999。此外正式環境還應處理 Esc 關閉、點擊遮罩關閉、開啟時鎖定背景捲動,以及焦點移入對話框,這幾點對鍵盤與螢幕閱讀器使用者是必要的。

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