Files
forumlify/js/user.js
T
Furrium Network 7176bea12c feat(ui): 重做页面转场为推入式,旧页面下沉、新页面从屏幕外滑入
原先的「滑入」只是 translateX(40px),几十像素的位移,在 1280px 宽的
屏上几乎看不出来,不是真正的页面转场。

现改为两段协同的推入效果:

1. 新页面(.page-slide)从屏幕外整体滑入:translateX(100%) → 0,
   同时 scale 0.94 → 1、opacity 0 → 1。用百分比而非固定像素,
   任何视口宽度下都从边缘之外开始。
2. 旧内容(#app)留在原位下沉:scale 1 → 0.94、右移 4%,
   并叠加一层暗色遮罩(#app::after,opacity 0 → 1)。

关键改动:#app 不再被 display:none 直接隐藏——那样根本没有动画可做。
现由 body.page-stack-open 与 .page-stack-sink 控制下沉,转场结束后
才清理状态。相关逻辑抽成 sinkBackground / riseBackground / showSlide /
hideSlide 四个函数,供 switchPage、showPostPage、showUserPage、
showCustomPage 共用。返回按钮走 navigateBackToFeed,与浏览器后退键
方向一致(新页面向左滑出、首页上浮)。

过程中修掉两个真实问题:

- 样式表靠后位置本有一条 `#app { animation: fadeIn ... }`(交互动效层)。
  ID 选择器优先级高于类选择器,导致最初的 .page-stack-sink 完全不生效,
  实测首页 transform 始终为 none。改为 #app.page-stack-sink 组合选择器
  后正常。reduced-motion 分支同样需要组合选择器才能压过它。
- app.js 中 showCustomPage 仍保留旧的 `#app.style.display = 'none'`,
  且给容器写了内联 display,会覆盖转场所需的 display 切换。已一并移除。

实测(Puppeteer 逐帧采样,视口 1280×900):
- 新页面 X 坐标:1280 → 860 → 541 → 421 → 326 → 60 → 6 → 0,
  起始 1280 恰好等于视口宽度,确认从屏幕外开始
- 首页:scale 1 → 0.940,右移 0 → 48px,遮罩不透明度 0 → 1.00
- 中间帧截图可见新页面自右侧滑入、旧页面在背景中缩小变暗

完整测试套件 25/25 通过。
2026-09-22 21:22:07 +08:00

94 lines
4.5 KiB
JavaScript

// ============================================================
// 👤 用户主页
// ============================================================
async function renderUserProfile(username) {
const container = document.getElementById('userProfileContent');
container.innerHTML = Skeleton.userProfile();
try {
// 使用公开接口 /users/profile/:username
const user = await apiFetch('/users/profile/' + encodeURIComponent(username));
if (user.error || !user.id) {
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">用户不存在</div>';
return;
}
document.getElementById('userPageTitle').textContent = '👤 ' + user.username;
const result = await apiFetch('/posts?user_id=' + user.id + '&page=1&limit=100');
const posts = result.data || [];
const avatar = user.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(user.username) + '&background=6366f1&color=fff&size=128';
let html = `
<div style="background:var(--surface);border:1px solid var(--border);border-radius:8px;padding:32px;text-align:center;">
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" style="width:96px;height:96px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);" />
<h2 style="margin:16px 0 4px;font-size:24px;">${escapeHTML(user.username)}</h2>
<p style="color:var(--text-secondary);font-size:14px;">${escapeHTML(user.bio || '这个人很懒,什么都没写')}</p>
<div style="display:flex;justify-content:center;gap:32px;margin-top:16px;font-size:14px;color:var(--text-secondary);flex-wrap:wrap;">
<span>📅 加入于 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
<span>📝 发了 ${posts.length} 个帖子</span>
${user.role === 'admin' ? '<span style="color:var(--primary);font-weight:600;">🛡️ 管理员</span>' : ''}
</div>
${currentUser && currentUser.id !== user.id ? `
<button id="dmBtn" class="btn-primary" style="margin-top:16px;padding:8px 24px;">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;margin-right:6px;"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
发私信
</button>
` : ''}
</div>
<h3 style="margin:24px 0 16px;font-size:18px;">📝 发布的帖子</h3>
`;
if (posts.length === 0) {
html += '<div style="color:#94a3b8;padding:20px 0;text-align:center;">还没有发帖</div>';
} else {
posts.forEach(p => {
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
html += `
<div class="post-card" data-postid="${escapeHTML(p.id)}" style="cursor:pointer;">
<div class="post-title" style="font-size:16px;font-weight:600;">${escapeHTML(p.title || '无标题')}</div>
<div class="post-content" style="font-size:14px;color:var(--text-secondary);">${escapeHTML((p.content || '').substring(0, 100))}${(p.content || '').length > 100 ? '...' : ''}</div>
<div style="font-size:12px;color:var(--text-light);margin-top:8px;">${time}</div>
</div>
`;
});
}
container.innerHTML = sanitizeHTML(html);
container.querySelectorAll('.post-card[data-postid]').forEach(card => {
card.addEventListener('click', function() {
switchToPost(this.dataset.postid);
});
});
const dmBtn = document.getElementById('dmBtn');
if (dmBtn) {
dmBtn.addEventListener('click', function() {
openPrivateChat(user.id, user.username);
});
}
} catch (err) {
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + escapeHTML(err.message) + '</div>';
}
}
function showUserPage(username) {
const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack();
const el = document.getElementById('pageUser');
// 先让其他覆盖页滑出(例如从帖子详情跳到用户主页)
document.querySelectorAll('.page-slide.active').forEach(other => {
if (other !== el) hideSlide(other, { back: goingBack });
});
// 首页留在背景中下沉,新页面从屏幕外滑入
showSlide(el, { back: goingBack });
currentPage = 'user';
renderUserProfile(username);
}