diff --git a/js/app.js b/js/app.js index 9d78480..2f522dd 100644 --- a/js/app.js +++ b/js/app.js @@ -3,6 +3,56 @@ // ============================================================ let currentUser = null; + +// ============================================================ +// 带出场动画的关闭 +// ============================================================ +// .modal 与 .dropdown-menu 的默认状态是 display:none,直接移除类名会让 +// 元素瞬间消失,CSS 动画没有机会播放。这里先挂上 .closing 播放收起 +// 动画,等 animationend 再移除类名恢复默认状态。 +// +// 用 requestAnimationFrame 等待样式生效后再挂类,避免元素在同一帧内 +// 被移除而跳过动画;animationend 同时兜底超时,防止动画未触发时卡住。 + +const CLOSING_CLASS = 'closing'; +const CLOSE_FALLBACK_MS = 260; + +function closeWithAnimation(el, className) { + if (!el || !el.classList.contains(className)) return; + // 已经在收起过程中则忽略重复调用 + if (el.classList.contains(CLOSING_CLASS)) return; + + let finished = false; + const finish = () => { + if (finished) return; + finished = true; + el.classList.remove(CLOSING_CLASS); + el.classList.remove(className); + el.removeEventListener('animationend', onEnd); + }; + const onEnd = event => { + if (event.target === el) finish(); + }; + + el.addEventListener('animationend', onEnd); + // 元素不可见(无动画)或动画被浏览器跳过时兜底 + const fallback = setTimeout(finish, CLOSE_FALLBACK_MS); + + requestAnimationFrame(() => { + // 若期间元素被重新打开(类名已被移除),取消本次关闭, + // 否则会把 .closing 挂到一个本应显示的弹窗上。 + if (!el.classList.contains(className) || finished) { + clearTimeout(fallback); + el.removeEventListener('animationend', onEnd); + return; + } + el.classList.add(CLOSING_CLASS); + }); +} + +const closeModal = el => closeWithAnimation(el, 'active'); +const closeDropdown = el => closeWithAnimation(el, 'show'); + function showToast(message, type = 'success', duration = 3000) { // 移除已有 toast const existing = document.querySelector('.toast'); @@ -202,7 +252,7 @@ function openMessageList() { } function closeMessageList() { - document.getElementById('messageListModal').classList.remove('active'); + closeModal(document.getElementById('messageListModal')); if (messagePollInterval) { clearInterval(messagePollInterval); messagePollInterval = null; @@ -272,7 +322,7 @@ function openChat(conversationId, otherUserId, otherUsername) { } function closeChat() { - document.getElementById('chatModal').classList.remove('active'); + closeModal(document.getElementById('chatModal')); if (messagePollInterval) { clearInterval(messagePollInterval); messagePollInterval = null; @@ -1176,7 +1226,7 @@ async function init() { e.preventDefault(); e.stopPropagation(); toggleTheme(e); - document.getElementById('dropdownMenu').classList.remove('show'); + closeDropdown(document.getElementById('dropdownMenu')); }); } @@ -1185,7 +1235,7 @@ async function init() { document.getElementById('dropdownMenu').classList.toggle('show'); }); document.addEventListener('click', function() { - document.getElementById('dropdownMenu').classList.remove('show'); + closeDropdown(document.getElementById('dropdownMenu')); }); document.querySelectorAll('[data-page]').forEach(el => { @@ -1196,7 +1246,7 @@ async function init() { alert('无权限访问'); return; } - document.getElementById('dropdownMenu').classList.remove('show'); + closeDropdown(document.getElementById('dropdownMenu')); switchPage(page); }); }); @@ -1243,7 +1293,7 @@ async function init() { statusEl.innerHTML = getIcon('success') + ' 重置成功!请登录'; statusEl.style.color = '#22c55e'; setTimeout(() => { - document.getElementById('forgotPasswordModal').classList.remove('active'); + closeModal(document.getElementById('forgotPasswordModal')); document.getElementById('resetEmail').value = ''; document.getElementById('resetRecoveryCode').value = ''; document.getElementById('resetNewPassword').value = ''; @@ -1346,7 +1396,7 @@ async function init() { const reason = document.getElementById('reportReason').value; try { await API.createReport(reportTargetPostId, reason); - document.getElementById('reportModal').classList.remove('active'); + closeModal(document.getElementById('reportModal')); alert('举报已提交,管理员将尽快处理'); reportTargetPostId = null; } catch (err) { @@ -1357,12 +1407,12 @@ async function init() { // ===== 模态框关闭 ===== document.querySelectorAll('.modal .close').forEach(btn => { btn.addEventListener('click', function() { - document.getElementById(this.dataset.modal).classList.remove('active'); + closeModal(document.getElementById(this.dataset.modal)); }); }); document.querySelectorAll('.modal').forEach(m => { m.addEventListener('click', function(e) { - if (e.target === this) this.classList.remove('active'); + if (e.target === this) closeModal(this); }); }); @@ -1419,7 +1469,7 @@ async function init() { document.querySelectorAll('.modal').forEach(m => { m.addEventListener('click', function(e) { if (e.target === this) { - this.classList.remove('active'); + closeModal(this); if (this.id === 'messageListModal') { closeMessageList(); } diff --git a/js/auth.js b/js/auth.js index 5b7b63b..ceec9b9 100644 --- a/js/auth.js +++ b/js/auth.js @@ -13,7 +13,7 @@ document.getElementById('loginSubmit').addEventListener('click', async () => { if (!email || !password) { alert('请填写完整信息'); return; } try { const result = await API.login(email, password); - document.getElementById('loginModal').classList.remove('active'); + closeModal(document.getElementById('loginModal')); document.getElementById('loginEmail').value = ''; document.getElementById('loginPassword').value = ''; if (result.user) { @@ -76,7 +76,7 @@ document.getElementById('registerSubmit').addEventListener('click', async () => return; } - document.getElementById('registerModal').classList.remove('active'); + closeModal(document.getElementById('registerModal')); document.getElementById('regUsername').value = ''; document.getElementById('regEmail').value = ''; document.getElementById('regPassword').value = ''; diff --git a/style.css b/style.css index 4634209..bed71ba 100644 --- a/style.css +++ b/style.css @@ -233,6 +233,8 @@ svg { position: absolute; right: 0; top: 44px; + /* 背景与边框由下方「用户下拉菜单」区块按主题分别定义(带 !important), + 这里只保留不影响该覆盖的结构性样式。 */ background: var(--glass-bg); backdrop-filter: blur(24px); -webkit-backdrop-filter: blur(24px); @@ -248,6 +250,13 @@ svg { display: block; animation: dropdownFade 0.15s ease-out; } +/* 关闭:JS 先挂 .closing 播放收起动画,动画结束再移除 .show。 + 若同时移除类名,display 会立刻变为 none,动画无从播放。 */ +.dropdown-menu.closing { + display: block; + animation: dropdownFadeOut 0.13s ease-in forwards; + pointer-events: none; +} @keyframes dropdownFade { from { opacity: 0; @@ -258,6 +267,16 @@ svg { transform: translateY(0) scale(1); } } +@keyframes dropdownFadeOut { + from { + opacity: 1; + transform: translateY(0) scale(1); + } + to { + opacity: 0; + transform: translateY(-6px) scale(0.98); + } +} .dropdown-menu a { display: block; padding: 10px 18px; @@ -266,9 +285,6 @@ svg { font-size: 14px; transition: background var(--transition); } -.dropdown-menu a:hover { - background: rgba(255, 255, 255, 0.15); -} .dropdown-menu a svg { margin-right: 8px; } @@ -581,6 +597,16 @@ svg { display: flex; animation: modalFade 0.2s ease-out; } +/* 关闭:类名移除时播放淡出。display 的切换无法过渡,因此改用 + animation 承接;JS 会延迟一帧再真正移除 .active 之外的内容。 */ +.modal.closing { + display: flex; + animation: modalFadeOut 0.16s ease-in forwards; + pointer-events: none; +} +.modal.closing .modal-content { + animation: modalPopOut 0.16s ease-in forwards; +} @keyframes modalFade { from { opacity: 0; @@ -591,11 +617,31 @@ svg { transform: scale(1); } } +@keyframes modalFadeOut { + from { opacity: 1; } + to { opacity: 0; } +} +@keyframes modalPopOut { + from { + opacity: 1; + transform: translateY(0) scale(1); + } + to { + opacity: 0; + transform: translateY(6px) scale(0.98); + } +} .modal-content { - background: var(--glass-bg); + /* 亮色模式用纯白底。原先沿用 --glass-bg(18% 不透明度),叠在模糊 + 背景上几乎全透明,文字与输入框都难以辨认。 + 此前只为 #messageListModal / #chatModal / #editorModal 单独做过 + 「亮色修复」(见文件下方注释),其余模态框都遗漏了;这里统一补上, + 取值与那几处保持一致(#ffffff)。 + 暗色模式仍沿用原变量,见下方覆盖。 */ + background: #ffffff; backdrop-filter: blur(32px); -webkit-backdrop-filter: blur(32px); - border: 1px solid var(--glass-border); + border: 1px solid #e9edf4; border-radius: var(--radius); padding: 32px 36px; max-width: 420px; @@ -605,6 +651,12 @@ svg { position: relative; box-shadow: 0 24px 80px rgba(0, 0, 0, 0.2); } + +/* 暗色模式保持原有玻璃质感,不做改动 */ +[data-theme="dark"] .modal-content { + background: var(--glass-bg); + border-color: var(--glass-border); +} .modal-content .close { position: absolute; top: 14px; @@ -637,10 +689,13 @@ svg { .modal-content select { width: 100%; padding: 10px 14px; - background: rgba(255, 255, 255, 0.12); - backdrop-filter: blur(8px); - -webkit-backdrop-filter: blur(8px); - border: 1px solid var(--glass-border); + /* 模态框已改为白底,输入框改用极浅灰底 + 浅灰描边,与背景区分。 + 原先的半透明白叠在白底上完全看不出边界。 + 边框色 #e9edf4 与文件下方几处「亮色修复」保持一致。 */ + background: rgba(0, 0, 0, 0.03); + backdrop-filter: none; + -webkit-backdrop-filter: none; + border: 1px solid #d8dfe9; border-radius: var(--radius-sm); font-size: 14px; color: var(--glass-text); @@ -652,7 +707,7 @@ svg { .modal-content textarea:focus, .modal-content select:focus { outline: none; - background: rgba(255, 255, 255, 0.25); + background: rgba(255, 255, 255, 0.9); border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-glow); } @@ -664,6 +719,9 @@ svg { color: var(--glass-text-light); } +/* 暗色模式的输入框覆盖见下方「暗色模式下的特殊修复」区块 + ([data-theme="dark"] .modal-content input ...),此处不再重复定义。 */ + .captcha-row { display: flex; align-items: center; @@ -671,7 +729,7 @@ svg { margin-bottom: 14px; } .captcha-row span { - background: rgba(255, 255, 255, 0.15); + background: rgba(0, 0, 0, 0.04); padding: 6px 16px; border-radius: var(--radius-sm); font-size: 16px; @@ -681,6 +739,9 @@ svg { user-select: none; transition: background var(--transition); } +[data-theme="dark"] .captcha-row span { + background: rgba(255, 255, 255, 0.15); +} .captcha-row span:hover { background: rgba(255, 255, 255, 0.3); } @@ -1331,6 +1392,9 @@ svg { [data-theme="dark"] .modal-content textarea, [data-theme="dark"] .modal-content select { background: rgba(255, 255, 255, 0.06); + /* 亮色模式改用了深色边框 rgba(0,0,0,.12),暗色下需换回浅色, + 否则输入框边界在深色背景上不可见。 */ + border-color: var(--glass-border); } [data-theme="dark"] .modal-content input:focus, [data-theme="dark"] .modal-content textarea:focus,