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:
1 parent
f982b2e0a6
commit
2f9528939a
3 files changed
+137
-23
No files matched your search
@@ -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
@@ -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 = '';
|
||||
|
||||
Reference in new issue
Block a user