alert/confirm,提供友好、可定制的彈出體驗。/**
* 彈出層基類
* @param {Object} options 配置選項
*/
function Dialog(options) {
this.options = Object.assign({
width: 'auto',
height: 'auto',
title: '',
content: '',
className: '',
modal: true, // 是否遮罩
draggable: false, // 是否可拖拽
onShow: null, // 顯示回調
onHide: null, // 隱藏回調
onConfirm: null, // 確認回調
onCancel: null // 取消回調
}, options);
this._init();
this._render();
this._bindEvents();
}
Dialog.prototype = {
constructor: Dialog,
_init: function() {
this.isOpen = false;
this.$mask = null;
this.$dialog = null;
this.$titleBar = null;
this.$content = null;
this.$footer = null;
},
_render: function() {
// 創建遮罩
if (this.options.modal) {
this.$mask = document.createElement('div');
this.$mask.className = 'dialog-mask';
document.body.appendChild(this.$mask);
}
// 創建對話框容器
this.$dialog = document.createElement('div');
this.$dialog.className = 'dialog ' + (this.options.className || '');
this.$dialog.style.width = this.options.width;
this.$dialog.style.height = this.options.height;
// 標題欄
if (this.options.title) {
this.$titleBar = document.createElement('div');
this.$titleBar.className = 'dialog-title';
this.$titleBar.innerHTML = '' + this.options.title + '';
this.$dialog.appendChild(this.$titleBar);
}
// 內容區
this.$content = document.createElement('div');
this.$content.className = 'dialog-content';
this.$content.innerHTML = this.options.content;
this.$dialog.appendChild(this.$content);
// 底部按鈕區
this.$footer = document.createElement('div');
this.$footer.className = 'dialog-footer';
this.$dialog.appendChild(this.$footer);
document.body.appendChild(this.$dialog);
},
_bindEvents: function() {
var self = this;
// 關閉按鈕
if (this.$titleBar) {
this.$titleBar.querySelector('.dialog-close').addEventListener('click', function() {
self.hide('cancel');
});
}
// 遮罩點擊關閉
if (this.$mask) {
this.$mask.addEventListener('click', function() {
self.hide('cancel');
});
}
// ESC 鍵關閉
this._escHandler = function(e) {
if (e.key === 'Escape' || e.keyCode === 27) self.hide('cancel');
};
document.addEventListener('keydown', this._escHandler);
// 拖拽(可選)
if (this.options.draggable && this.$titleBar) {
this._enableDrag();
}
},
_enableDrag: function() {
var self = this, dragging = false, offsetX, offsetY;
this.$titleBar.style.cursor = 'move';
this.$titleBar.addEventListener('mousedown', function(e) {
dragging = true;
offsetX = e.clientX - self.$dialog.offsetLeft;
offsetY = e.clientY - self.$dialog.offsetTop;
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onUp);
});
function onMove(e) {
if (!dragging) return;
self.$dialog.style.left = (e.clientX - offsetX) + 'px';
self.$dialog.style.top = (e.clientY - offsetY) + 'px';
}
function onUp() {
dragging = false;
document.removeEventListener('mousemove', onMove);
document.removeEventListener('mouseup', onUp);
}
},
// 居中顯示
_center: function() {
var dw = this.$dialog.offsetWidth;
var dh = this.$dialog.offsetHeight;
var vw = window.innerWidth || document.documentElement.clientWidth;
var vh = window.innerHeight || document.documentElement.clientHeight;
this.$dialog.style.left = Math.max(0, (vw - dw) / 2) + 'px';
this.$dialog.style.top = Math.max(0, (vh - dh) / 2) + 'px';
},
// 公開 API
show: function() {
if (this.isOpen) return this;
this._center();
this.$dialog.style.display = 'block';
if (this.$mask) this.$mask.style.display = 'block';
this.isOpen = true;
if (this.options.onShow) this.options.onShow.call(this);
// 窗口 resize 重新居中
window.addEventListener('resize', this._center.bind(this));
return this;
},
hide: function(result) {
if (!this.isOpen) return this;
this.$dialog.style.display = 'none';
if (this.$mask) this.$mask.style.display = 'none';
this.isOpen = false;
window.removeEventListener('resize', this._center.bind(this));
document.removeEventListener('keydown', this._escHandler);
if (result === 'confirm' && this.options.onConfirm) this.options.onConfirm.call(this);
if (result === 'cancel' && this.options.onCancel) this.options.onCancel.call(this);
if (this.options.onHide) this.options.onHide.call(this, result);
return this;
},
confirm: function() { return this.hide('confirm'); },
cancel: function() { return this.hide('cancel'); },
setContent: function(html) {
this.$content.innerHTML = html;
this._center(); // 內容變化可能改變尺寸
return this;
},
setTitle: function(title) {
if (this.$titleBar) this.$titleBar.querySelector('span').textContent = title;
return this;
},
destroy: function() {
this.hide();
if (this.$mask) this.$mask.parentNode.removeChild(this.$mask);
if (this.$dialog) this.$dialog.parentNode.removeChild(this.$dialog);
this.$mask = this.$dialog = this.$titleBar = this.$content = this.$footer = null;
}
};
.dialog-mask {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.5); z-index: 9998; display: none;
}
.dialog {
position: fixed; z-index: 9999; display: none;
background: #fff; border-radius: 4px; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.dialog-title { padding: 12px 16px; border-bottom: 1px solid #eee; display: flex; justify-content: space-between; align-items: center; }
.dialog-close { background: none; border: none; font-size: 20px; cursor: pointer; color: #999; }
.dialog-content { padding: 16px; overflow: auto; }
.dialog-footer { padding: 12px 16px; border-top: 1px solid #eee; text-align: right; }
// 1. 簡單載入提示
var loading = new Dialog({
title: '處理中',
content: '<div class="spinner"></div><p>請稍候...</p>',
width: '300px',
modal: true
}).show();
// 2. 確認框
new Dialog({
title: '確認刪除',
content: '確定要刪除這條記錄嗎?此操作不可撤銷。',
onConfirm: function() { console.log('用戶確認'); api.delete(id); },
onCancel: function() { console.log('用戶取消'); }
}).show();
// 3. 表單彈窗
var formDialog = new Dialog({
title: '編輯用戶',
content: '<form id="userForm">...</form>',
width: '500px',
onConfirm: function() {
var data = $('#userForm').serialize();
api.updateUser(data).then(() => this.hide('confirm'));
return false; // 阻止自動關閉,由異步決定
}
}).show();
transition + transform: scale(0.9) → scale(1)。DialogManager 維護 z-index 遞增、Esc 關閉頂層。role="dialog"、aria-modal="true"、焦點陷阱、aria-labelledby 指向標題。面向對象封裝讓彈出層邏輯內聚、復用性強、易於測試與擴展,是組件化思維在傳統 JS 專案中的經典落地。