fix(ui): 模态框亮色模式改白底,并为弹窗与菜单补关闭动画

两个问题一起处理。

一、亮色模式下大部分模态框近乎透明

.modal-content 一直沿用 --glass-bg(亮色下为 rgba(255,255,255,0.18)),
叠在模糊背景上几乎全透明,标题、输入框与按钮都难以辨认。

文件下方其实已有三处「亮色修复」(#messageListModal / #chatModal /
#editorModal),用 #ffffff !important 单独处理过——说明这个问题此前
被发现了,但只修了这三个,登录、注册、发帖、举报、设置等其余模态框
全部遗漏。这里在通用规则上统一补上,取值与那几处保持一致(#ffffff),
暗色模式仍沿用原变量不变。

输入框同步调整:白底上用 3% 黑底 + #d8dfe9 描边,替代原先叠在白底上
完全看不出边界的半透明白。暗色输入框由文件下方既有规则接管,此处不再
重复定义(原先误加过一份,已移除)。

二、关闭弹窗与下拉菜单没有动画

.modal 与 .dropdown-menu 的默认状态是 display:none,JS 直接
classList.remove('active'/'show') 会让元素瞬间消失,CSS 无从播放动画。

改为先挂 .closing 播放收起动画(modalFadeOut / modalPopOut /
dropdownFadeOut),animationend 后再移除类名。要点:

- 用 requestAnimationFrame 等样式生效后再挂类,避免同帧移除跳过动画
- 同时设 260ms 兜底超时,防止动画未触发(如系统开启了减少动画)时卡住
- rAF 回调中检查类名是否已被移除,避免关闭过程中被重新打开时误挂
  .closing
- 新增 closeModal() / closeDropdown() 两个辅助函数,替换原先散落在
  app.js 与 auth.js 中的 9 处直接移除类名的写法
- 标签页选中态(.tab / .admin-nav-item / .page-slide 等)不适用此逻辑,
  保持原样

关闭动画同样受 prefers-reduced-motion 约束(时长被压到 0.01ms,兜底
超时保证状态正常收尾)。

已用 Puppeteer 实测:亮色下 loginModal / registerModal / reportModal
背景均为 rgb(255,255,255),暗色下均为 rgba(30,30,50,0.35) 保持原样,
已被单独修过的 messageListModal 不受影响;关闭时 .closing 正确挂载并
播放 modalFadeOut / dropdownFadeOut,动画结束后 display 回到 none。
This commit is contained in:
Furrium Network committed 2026-09-21 21:03:03 +08:00
1 parent f982b2e0a6
commit 2f9528939a
3 files changed
+137 -23

No files matched your search

+60 -10
View File
@@ -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();
}
+2 -2
View File
@@ -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 = '';
+75 -11
View File
@@ -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,