/** * 自定义模态框组件 * 符合项目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 + `
${this.title || this.showClose ? `
${this.title ? `
${this.title}
` : ''} ${this.showClose ? `` : ''}
` : ''}
${this.content}
${this.showFooter ? ` ` : ''}
`; } _bindEvents() { // 关闭按钮 const closeBtn = this.modal.querySelector('[data-dismiss="modal"]'); if (closeBtn) { closeBtn.addEventListener('click', () => this.hide()); } // 确认按钮 const confirmBtn = this.modal.querySelector('.custom-modal-btn-confirm'); if (confirmBtn && this.onConfirm) { confirmBtn.addEventListener('click', () => { if (this.onConfirm() !== false) { this.hide(); } }); } // 点击背景关闭 if (this.backdropEl) { this.backdropEl.addEventListener('click', () => this.hide()); } // 点击内容区域不关闭 const content = this.modal.querySelector('.custom-modal-content'); if (content) { content.addEventListener('click', (e) => e.stopPropagation()); } } updateContent(content) { const body = this.modal?.querySelector('.custom-modal-body'); if (body) { body.innerHTML = content; } } } // CSS样式(通过JavaScript注入,或单独引入CSS文件) const customModalStyles = ` `; // 注入样式 if (!document.getElementById('custom-modal-styles')) { document.head.insertAdjacentHTML('beforeend', customModalStyles); } // 导出到全局 window.CustomModal = CustomModal; // 便捷方法 window.showCustomModal = function(options) { const modal = new CustomModal(options); modal.show(); return modal; }; window.showCustomConfirm = function(message, title = '确认操作', onConfirm = null) { return new CustomModal({ title: title, content: `

${message}

`, onConfirm: onConfirm, showFooter: true }); };