DIV CSS 佈局教程網

用 jQuery 實作模態彈出層:遮罩、置中、鎖捲軸與 select 穿透一次解決
編輯:JQuery特效代碼     

折騰了好幾回,終於做出一個堪用的模態層。效果如下:

步驟一:撰寫 jquery.divbox.js

jQuery.fn.extend({
    OpenDiv: function () {
        var sWidth, sHeight;
        sWidth = window.screen.availWidth;
        if (window.screen.availHeight > document.body.scrollHeight) {
            sHeight = window.screen.availHeight;
        } else {
            sHeight = document.body.scrollHeight + 20;
        }

        var maskObj = document.createElement("div");
        maskObj.setAttribute('id', 'BigDiv');
        maskObj.style.position = "absolute";
        maskObj.style.top = "0";
        maskObj.style.left = "0";
        maskObj.style.background = "#111";
        maskObj.style.filter = "Alpha(opacity=70);";
        maskObj.style.opacity = "0.7";
        maskObj.style.width = sWidth + "px";
        maskObj.style.height = sHeight + "px";
        maskObj.style.zIndex = "10000";

        $("body").attr("scroll", "no");
        document.body.appendChild(maskObj);

        // 暫存被遮罩影響的 select,稍後恢復
        $("#BigDiv").data("divbox_selectlist", $("select:visible"));
        $("select:visible").hide();

        $("#BigDiv").attr("divbox_scrolltop", $.ScrollPosition().Top);
        $("#BigDiv").attr("divbox_scrollleft", $.ScrollPosition().Left);
        $("#BigDiv").attr("htmloverflow", $("html").css("overflow"));
        $("html").css("overflow", "hidden");
        window.scrollTo($("#BigDiv").attr("divbox_scrollleft"), $("#BigDiv").attr("divbox_scrolltop"));

        var MyDiv_w = parseInt(this.width());
        var MyDiv_h = parseInt(this.height());
        var width = $.PageSize().Width;
        var height = $.PageSize().Height;
        var left = $.ScrollPosition().Left;
        var top = $.ScrollPosition().Top;
        var Div_topposition = top + (height / 2) - (MyDiv_h / 2);
        var Div_leftposition = left + (width / 2) - (MyDiv_w / 2);

        this.css("position", "absolute");
        this.css("z-index", "10001");
        this.css("background", "#fff");
        this.css("left", Div_leftposition + "px");
        this.css("top", Div_topposition + "px");

        if ($.browser.mozilla) {
            this.show();
            return;
        }
        this.fadeIn("fast");
    },

    CloseDiv: function () {
        if ($.browser.mozilla) {
            this.hide();
        } else {
            this.fadeOut("fast");
        }
        $("html").css("overflow", $("#BigDiv").attr("htmloverflow"));
        window.scrollTo($("#BigDiv").attr("divbox_scrollleft"), $("#BigDiv").attr("divbox_scrolltop"));
        $("#BigDiv").data("divbox_selectlist").show();
        $("#BigDiv").remove();
    }
});

$.extend({
    PageSize: function () {
        var width = 0, height = 0;
        width = window.innerWidth != null ? window.innerWidth
              : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth
              : document.body != null ? document.body.clientWidth : null;
        height = window.innerHeight != null ? window.innerHeight
               : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight
               : document.body != null ? document.body.clientHeight : null;
        return { Width: width, Height: height };
    },
    ScrollPosition: function () {
        var top = 0, left = 0;
        if ($.browser.mozilla) {
            top = window.pageYOffset;
            left = window.pageXOffset;
        } else if ($.browser.msie) {
            top = document.documentElement.scrollTop;
            left = document.documentElement.scrollLeft;
        } else if (document.body) {
            top = document.body.scrollTop;
            left = document.body.scrollLeft;
        }
        return { Top: top, Left: left };
    }
});

步驟二:引入兩個檔案

頁面需依序載入 jquery.js 與 jquery.divbox.js。

步驟三:測試頁面

<html>
<head>
<title>測試</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.divbox.js"></script>
<style>
#divSCA {
    position: absolute;
    width: 700px;
    height: 500px;
    font-size: 12px;
    background: #fff;
    border: 0px solid #000;
    z-index: 10001;
    display: none;
}
</style>
<script type="text/javascript">
function openDiv() { $("#divSCA").OpenDiv(); }
function closeDiv() { $("#divSCA").CloseDiv(); }
</script>
</head>
<body>
<div id="divSCA">
<font size="50">這是模態DIV,點擊關閉</font>
<input type="button" value="關閉" onclick="closeDiv()">
</div>
<input type="button" value="彈出" onclick="openDiv()"/>
測試能否覆蓋 select
<select>
<option>測試1</option>
<option>測試2</option>
<option>測試3</option>
</select>
</body>
</html>

幾個設計細節說明

  • 遮罩尺寸取「視窗與內容的較大者」:確保內容很長時遮罩仍能蓋滿整個可捲動區域。
  • 鎖住捲軸:把 html 的 overflow 暫存後設為 hidden,關閉時再還原,避免背景滑動。
  • 記錄並還原捲動位置:因為鎖捲軸會導致位置跳動,所以先存 scrollLeft/scrollTop,關閉後 scrollTo 回去。
  • select 穿透的老問題:在舊版 IE 中,select 是 window 級元件,會蓋在任何 z-index 之上。這裡的做法是暫存並隱藏所有可見 select,關閉時再顯示——屬於無可奈何的相容手段。

今日回看這份代碼

這份實作有幾處可以改進:$.browser 已在 jQuery 1.9 移除,應改用功能偵測;用 attr 暫存資料建議改用 data();至于 select 穿透問題,現代瀏覽器已不存在,而 position:fixed 加上適當的 overflow 設定,就能省下大半的跨瀏覽器判斷。不過作為一次完整的練習,把每個坑都親手踩過一遍,收穫反而更紮實。

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