萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JavaScript 跨視窗屏幕鎖定與模擬對話框實作

應用場景

在 iframe 或子視窗中,需鎖定父視窗整個畫面並顯示模態對話框(如確認刪除、載入中遮罩)。本文示範純 JavaScript 動態建立遮罩層與對話框,並解決跨視窗 DOM 操作、IE 相容性、透明度設定等關鍵細節。

核心實作:sAlert() 函式

function sAlert() {
    // 1. 尺寸與樣式參數
    var msgw = 450, msgh = 120, titleheight = 25;
    var bordercolor = "#104E8B";

    // 2. 取得螢幕寬高(父視窗)
    var sWidth  = screen.width;
    var sHeight = screen.height;

    // 3. 建立遮罩層 (bgDiv)
    var bgObj = window.parent.document.createElement("div");
    bgObj.setAttribute('id', 'bgDiv');
    bgObj.style.cssText = [
        'position:absolute', 'top:0', 'left:0',
        'width:' + sWidth + 'px', 'height:' + sHeight + 'px',
        'background:#cccccc', 'zIndex:10000',
        'opacity:0.6', 'filter:alpha(opacity=60)'  // IE 透明度相容
    ].join(';');
    window.parent.document.body.appendChild(bgObj);

    // 4. 建立對話框容器 (msgDiv)
    var msgObj = window.parent.document.createElement("div");
    msgObj.setAttribute("id", "msgDiv");
    msgObj.style.cssText = [
        'position:absolute', 'left:50%', 'top:50%',
        'margin-left:-225px', 'margin-top:' + (-75 + document.documentElement.scrollTop) + 'px',
        'width:' + msgw + 'px', 'height:' + msgh + 'px',
        'background:white', 'border:1px solid ' + bordercolor,
        'zIndex:10001', 'text-align:center', 'line-height:25px',
        'font:12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif'
    ].join(';');

    // 5. 建立標題列 (h4)
    var title = window.parent.document.createElement("h4");
    title.setAttribute("id", "msgTitle");
    title.style.cssText = [
        'margin:0', 'padding:3px', 'height:18px',
        'background:' + bordercolor, 'border:1px solid ' + bordercolor,
        'color:white', 'cursor:pointer',
        'font:12px Verdana, Geneva, Arial, Helvetica, sans-serif',
        'opacity:0.75', 'filter:progid:DXImageTransform.Microsoft.Alpha(startX=20,startY=20,finishX=100,finishY=100,style=1,opacity=75,finishOpacity=100)'
    ].join(';');
    title.innerHTML = '提示訊息'; // 可動態傳入

    // 6. 組裝並加入父視窗 body
    msgObj.appendChild(title);
    window.parent.document.body.appendChild(msgObj);
}

關鍵技術點解析

1. 跨視窗 DOM 操作必須使用 window.parent

子視窗(iframe)若要操作父視窗文件,所有 document.createElement、appendChild 等呼叫皆需加上 window.parent. 前綴,否則 IE 會拋出「存取被拒」錯誤,因為元素必須屬於建立它的文件。

2. IE 相容性處理

  • setAttribute('class', ...):IE 不支援,需改用 element.className = '...'。
  • 透明度:現代瀏覽器用 opacity:0.6,IE 8- 需 filter:alpha(opacity=60);兩行並寫互不干擾。
  • 事件模型:若需綁定關閉事件,IE 用 attachEvent,標準瀏覽器用 addEventListener。

3. 置中定位計算

對話框採用 left:50%; top:50% 配合負 margin-left(寬度一半)與負 margin-top(高度一半 + 捲軸位移)實現畫面置中,需注意 document.documentElement.scrollTop 在不同文型宣告下的差異。

完善建議:封裝為可復用模組

var Modal = (function(win) {
    var doc = win.document, bgDiv, msgDiv;

    function createOverlay() {
        bgDiv = doc.createElement('div');
        bgDiv.id = 'modal-overlay';
        bgDiv.style.cssText = 'position:fixed;top:0;left:0;width:100%;height:100%;background:#000;opacity:0.5;z-index:9999;';
        doc.body.appendChild(bgDiv);
    }

    function createDialog(html, opts) {
        msgDiv = doc.createElement('div');
        msgDiv.id = 'modal-dialog';
        msgDiv.innerHTML = html;
        msgDiv.style.cssText = 'position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);background:#fff;padding:20px;border-radius:4px;z-index:10000;min-width:300px;max-width:90%;box-shadow:0 2px 10px rgba(0,0,0,.3);';
        doc.body.appendChild(msgDiv);
    }

    return {
        open: function(html, options) {
            createOverlay();
            createDialog(html, options);
        },
        close: function() {
            if (bgDiv) bgDiv.remove();
            if (msgDiv) msgDiv.remove();
        }
    };
})(window.parent); // 傳入目標視窗

// 子視窗呼叫
// Modal.open('<h3>確定刪除?</h3><button onclick="Modal.close()">確定</button>');

總結

跨視窗模態框的關鍵在於:所有 DOM 操作鎖定目標視窗的 document、妥善處理 IE 透明度與 class 設定相容性、正確計算置中座標含捲軸位移。現代專案建議直接使用 position:fixed + transform:translate(-50%,-50%) 簡化置中邏輯,並封裝為模組化 API 方便複用。

copyright © 萬盛學電腦網 all rights reserved