寫下這篇文章,是想把想法分享給大家。初學者能從中獲益,經驗豐富的開發者也能指正不足,共同進步。
目標是實作一個外掛,取代瀏覽器預設的彈出對話框(window.alert、confirm、prompt)與彈出視窗(window.open、showModalDialog 等)。因為預設對話框功能簡單、使用者體驗差,而現代瀏覽器又多會阻擋彈出視窗。
無論在哪個瀏覽器,顯示的樣式與佈局都保持一致,並固定於視窗正中央,讓使用者能第一時間看到。

1、遮擋頁面內容(半透明遮罩),透明度可設定,讓使用者在關閉對話框前無法操作頁面。
2、對話框置中顯示,大小可設定。
3、圖標可設定(標題列小圖與內容左側大圖)。
4、標題與內容可設定。
5、可選擇不顯示關閉按鈕。
6、底部按鈕可為 0 或多個,並可各自設定回調函數。
1. 用 CSS 控制外觀,並以命名空間避免樣式衝突。
2. 遮罩層以絕對定位鋪滿,z-index 有安全範圍(跨瀏覽器建議不超過 16777271),寬高則用 $(document).width() 與 $(document).height() 取得。
3. 置中顯示可用 position:fixed(滾動時仍居中,但不支援 IE6),或以 JS 計算座標定位。
要做到完全相容所有瀏覽器需花費大量時間。以 IE6 為例,它在全球使用率已低於 5%,卻在部分地區仍佔相當比例。為它多花三分之一甚至更多的時間是否值得,值得每個開發者思考。
<style type="text/css">
.ctcx-dialog{font-size:14px;}
.ctcx-dialog .mark{position:absolute;left:0;top:0;background-color:#000;z-index:99999;}
.ctcx-dialog .dialog{position:fixed;left:50%;top:50%;background-color:#FFF;z-index:99999;border:2px solid #000;padding:2px;}
.ctcx-dialog .dialog .bar{height:30px;background-color:#999;color:#FFF;}
.ctcx-dialog .dialog .bar .icon{width:25px;height:30px;background-repeat:no-repeat;background-position:center;display:inline-block;}
.ctcx-dialog .dialog .bar .title{width:340px;height:30px;line-height:30px;overflow:hidden;display:inline-block;vertical-align:top;font-weight:bold;}
.ctcx-dialog .dialog .bar .close{width:20px;height:30px;background-image:url(close.png);background-repeat:no-repeat;background-position:center;display:inline-block;cursor:pointer;}
.ctcx-dialog .dialog .container{margin-top:5px;overflow:auto;}
.ctcx-dialog .dialog .container .icon{background-image:url(icon-big.png);background-repeat:no-repeat;background-position:center;width:48px;height:48px;}
.ctcx-dialog .dialog .container .content{position:relative;}
.ctcx-dialog .dialog .buttons{text-align:center;margin-top:5px;height:30px;position:relative;bottom:0px;}
.ctcx-dialog .dialog .buttons a{background-color:#DDD;color:#000;text-decoration:none;display:inline-block;padding:5px;}
.ctcx-dialog .dialog .buttons a:hover{background-color:#333;color:#FFF;}
</style>
JS 部分:
. 代碼如下:(function ($) {
$.alert = function (options) {
if (typeof options === 'string') options = { content: options };
var opts = $.extend({}, $.alert.defaults, options);
if (opts.content == null || opts.content.length == 0) return this;
var me = $('<div></div>').addClass('ctcx-dialog').appendTo(document.body);
var doc = $(document);
$('<div class="mark"></div>').css({ opacity: opts.opacity }).width(doc.width()).height(doc.height()).appendTo(me);
var _dialog_ = $('<div class="dialog"></div>').css({ width: opts.width, height: opts.height, marginLeft: 0 - opts.width / 2, marginTop: 0 - opts.height / 2 }).appendTo(me);
var _bar_ = $('<div class="bar"></div>').appendTo(_dialog_);
var _titleWidth_ = opts.width - 0;
if (opts.icon != null) { $('<div class="icon"></div>').css('background-image', 'url(' + opts.icon + ')').appendTo(_bar_); _titleWidth_ -= 25; }
if (opts.close) _titleWidth_ -= 20;
$('<div class="title"></div>').css({ width: _titleWidth_ }).html(opts.title).appendTo(_bar_);
if (opts.close) { $('<div class="close"></div>').click(function () { me.remove(); }).appendTo(_bar_); }
var _containerHeight_ = opts.height - 40;
var _container_ = $('<div class="container"></div>').appendTo(_dialog_);
var _contentCss_ = {};
if (opts.iconBig != null) { $('<div class="icon"></div>').css('background-image', 'url(' + opts.iconBig + ')').appendTo(_container_); _contentCss_.top = -48; _contentCss_.marginLeft = 48; }
var _content_ = $('<div class="content"></div>').css(_contentCss_).html(opts.content).appendTo(_container_);
if (opts.buttons != null && opts.buttons.length > 0) { _containerHeight_ -= 30; var _buttons_ = $('<div class="buttons"></div>').appendTo(_dialog_); $.each(opts.buttons, function (i, _button) { $('<a href="javascript:;">' + _button.text + '</a>').click(function () { _button.fn(me); }).appendTo(_buttons_); }) }
_container_.css({ height: _containerHeight_ });
this.close = function () { me.remove(); }
this.setContent = function (content) { _content_.html(content); }
return this;
}
$.alert.defaults = { title: '信息提示', content: null, width: 200, height: 100, opacity: 0.5, icon: null, iconBig: null, buttons: null, close: true }
})(jQuery);
呼叫範例:
. 代碼如下:$.alert({
title: '火星向你發出警告',
content: '我們是火星人,我們就要入侵地球了,你們准備好了嗎?',
width: 300,
height: 150,
opacity: 0.5,
icon: 'icon.png',
iconBig: 'icon-big.png',
buttons: [{ text: '好怕怕', fn: function () { $.alert('我好怕怕呀')} }],
close: true
});
七、下載:可下載測試與使用範例自行修改。