/** * 自定义模态框组件 * 符合项目UI设计规范,具有平滑过渡动画和响应式显示 */ class CustomModal { constructor(options = {}) { this.id = options.id || 'customModal_' + Date.now(); this.title = options.title || ''; this.content = options.content || ''; this.size = options.size || 'medium'; // small, medium, large this.showClose = options.showClose !== false; this.onClose = options.onClose || null; this.onConfirm = options.onConfirm || null; this.confirmText = options.confirmText || '确定'; this.cancelText = options.cancelText || '取消'; this.showFooter = options.showFooter !== false; this.backdrop = options.backdrop !== false; this.modal = null; } show() { // 创建模态框HTML const modalHtml = this._createModalHtml(); // 移除已存在的模态框 const existing = document.getElementById(this.id); if (existing) { existing.remove(); } // 添加到body document.body.insertAdjacentHTML('beforeend', modalHtml); this.modal = document.getElementById(this.id); this.backdropEl = document.getElementById(this.id + '_backdrop'); // 添加显示动画 requestAnimationFrame(() => { this.modal.classList.add('show'); if (this.backdropEl) { this.backdropEl.classList.add('show'); } document.body.style.overflow = 'hidden'; }); // 绑定事件 this._bindEvents(); } hide() { if (!this.modal) return; this.modal.classList.remove('show'); if (this.backdropEl) { this.backdropEl.classList.remove('show'); } document.body.style.overflow = ''; // 动画结束后移除 setTimeout(() => { if (this.modal && this.modal.parentNode) { this.modal.remove(); } if (this.backdropEl && this.backdropEl.parentNode) { this.backdropEl.remove(); } if (this.onClose) { this.onClose(); } }, 300); } _createModalHtml() { const sizeClass = { small: 'modal-sm', medium: '', large: 'modal-lg' }[this.size]; const backdropHtml = this.backdrop ? `
` : ''; return backdropHtml + `${message}
`, onConfirm: onConfirm, showFooter: true }); };