/** * Toast 3D Stack - 健壮版本 */ (function (global) { 'use strict'; const icons = { success: ``, error: ``, info: ``, warning: `` }; let toastId = 0; const MAX_STACK = 5; const DISPLAY_TIME = 4000; let isReady = false; const pendingQueue = []; const isMobile = window.matchMedia('(max-width: 640px)').matches; let touchStartY = 0; let isDragging = false; let currentToast = null; function checkReady() { return document.readyState === 'complete' || document.readyState === 'interactive'; } function ensureReady(callback) { if (isReady || checkReady()) { isReady = true; callback(); } else { document.addEventListener('DOMContentLoaded', () => { isReady = true; callback(); }, { once: true }); } } function getContainer() { let stage = document.getElementById('toast-stage'); if (!stage) { stage = document.createElement('div'); stage.id = 'toast-stage'; const container = document.createElement('div'); container.id = 'toast-container'; stage.appendChild(container); if (document.body) { document.body.appendChild(stage); } else { document.addEventListener('DOMContentLoaded', () => { document.body.appendChild(stage); }, { once: true }); } } return stage.querySelector('#toast-container'); } function createToast(message, type) { const el = document.createElement('div'); el.className = `toast ${type} entering`; el.dataset.id = ++toastId; el.innerHTML = `
${icons[type] || icons.info}
${escapeHtml(message)}
`; if (isMobile) { attachTouchEvents(el); } return el; } function attachTouchEvents(el) { el.addEventListener('touchstart', handleTouchStart, { passive: true }); el.addEventListener('touchmove', handleTouchMove, { passive: true }); el.addEventListener('touchend', handleTouchEnd, { passive: true }); el.addEventListener('click', () => removeToast(el)); } function handleTouchStart(e) { const touch = e.touches[0]; touchStartY = touch.clientY; isDragging = true; currentToast = e.currentTarget; currentToast.classList.add('swiping'); } function handleTouchMove(e) { if (!isDragging || !currentToast) return; const touch = e.touches[0]; const deltaY = touch.clientY - touchStartY; if (deltaY > 0) { const opacity = Math.max(0.3, 1 - deltaY / 200); currentToast.style.transform = `translateY(${deltaY}px) scale(${Math.max(0.95, 1 - deltaY / 1000)})`; currentToast.style.opacity = opacity; } } function handleTouchEnd(e) { if (!isDragging || !currentToast) return; const touch = e.changedTouches[0]; const deltaY = touch.clientY - touchStartY; currentToast.classList.remove('swiping'); if (deltaY > 80) { removeToast(currentToast); } else { currentToast.style.transform = ''; currentToast.style.opacity = ''; } isDragging = false; currentToast = null; } function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } function removeToast(el, immediate = false) { if (!el || el.classList.contains('exiting')) return; if (immediate) { el.remove(); return; } el.classList.add('exiting'); if (isMobile) { // 移动端出场:向下滑出 + 缩小,与入场对称 el.style.transform = 'translateY(20px) scale(0.95)'; el.style.opacity = '0'; el.style.transition = 'transform 0.3s ease, opacity 0.25s ease'; } else { el.style.transform = 'translateX(100px) translateZ(-100px) scale(0.8) rotateY(-15deg)'; el.style.opacity = '0'; el.style.transition = 'transform 0.35s ease, opacity 0.3s ease'; } el.style.pointerEvents = 'none'; setTimeout(() => { if (el.parentNode) el.remove(); }, isMobile ? 300 : 350); } function processShow(message, type) { const container = getContainer(); if (!container) { pendingQueue.push({ message, type }); return; } if (isMobile && container.children.length > 0) { Array.from(container.children).forEach(oldToast => removeToast(oldToast, true)); } const existingToasts = Array.from(container.children); if (!isMobile) { existingToasts.forEach(toast => toast.classList.add('shift-down')); } const el = createToast(message, type); container.prepend(el); // 强制同步重排 void el.offsetHeight; // 桌面端:强制设置初始状态 if (!isMobile) { el.style.transform = 'translateX(-120px) translateZ(50px) rotateY(25deg)'; el.style.opacity = '0'; el.style.transition = 'none'; } requestAnimationFrame(() => { void el.offsetHeight; requestAnimationFrame(() => { el.classList.add('show'); if (!isMobile) { el.style.transform = ''; el.style.opacity = ''; el.style.transition = ''; } }); }); setTimeout(() => { el.classList.remove('entering', 'show'); if (!isMobile) { el.style.cssText = ''; existingToasts.forEach(toast => { if (toast.parentNode) toast.classList.remove('shift-down'); }); } }, 600); if (!isMobile && container.children.length > MAX_STACK) { const oldest = container.lastElementChild; if (oldest) { oldest.style.transition = 'all 0.3s ease'; oldest.style.opacity = '0'; oldest.style.transform = 'translateZ(-250px) scale(0.7)'; setTimeout(() => oldest.remove(), 300); } } setTimeout(() => removeToast(el), DISPLAY_TIME); } function show(message, type = 'info') { ensureReady(() => processShow(message, type)); } function processQueue() { while (pendingQueue.length > 0) { const { message, type } = pendingQueue.shift(); processShow(message, type); } } if (checkReady()) { processQueue(); } else { window.addEventListener('load', processQueue); } global.Toast = { show, success: (msg) => show(msg, 'success'), error: (msg) => show(msg, 'error'), info: (msg) => show(msg, 'info'), warning: (msg) => show(msg, 'warning'), clear: () => { const container = getContainer(); if (container) { Array.from(container.children).forEach(el => removeToast(el, true)); } } }; global.showToast = (msg, type = 'success') => show(msg, type); })(window);