diff --git a/index.html b/index.html index ee6ad97..90c7c18 100644 --- a/index.html +++ b/index.html @@ -459,6 +459,7 @@ + diff --git a/js/admin.js b/js/admin.js index 00080f9..58f5f3f 100644 --- a/js/admin.js +++ b/js/admin.js @@ -73,7 +73,7 @@ let usersSearchKeyword = ''; function renderAdminUsers(page = 1, search = '') { const container = document.getElementById('adminContent'); - container.innerHTML = '
加载中...
'; + container.innerHTML = Skeleton.tableRows(6); currentUsersPage = page; usersSearchKeyword = search; @@ -262,8 +262,8 @@ function renderAdminUsers(page = 1, search = '') { function renderAdminLogs(page = 1) { const container = document.getElementById('adminContent'); - container.innerHTML = '
加载中...
'; - + container.innerHTML = Skeleton.tableRows(6); + API.getEventLogs(page, 20).then(result => { const logs = result.data || []; const pagination = result.pagination || { total: 0, totalPages: 1, page: 1 }; diff --git a/js/app.js b/js/app.js index 33e6a23..f81ef5c 100644 --- a/js/app.js +++ b/js/app.js @@ -252,7 +252,7 @@ function switchPage(page, param) { document.getElementById('app').style.display = 'none'; document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out'); + el.classList.remove('active', 'slide-out', 'slide-back'); }); const customContainer = document.getElementById('customPageContainer'); @@ -278,6 +278,11 @@ function switchPage(page, param) { }; const el = document.getElementById(pageMap[page]); if (el) { + // 后退导航时用反向滑入:返回上一页时内容应从左侧进来, + // 而主动点击进入是从右侧进来。 + if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { + el.classList.add('slide-back'); + } el.classList.add('active'); el.style.animation = 'none'; void el.offsetHeight; @@ -607,7 +612,7 @@ function renderCustomPagesNav() { function showCustomPage(pageName) { document.getElementById('app').style.display = 'none'; document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out'); + el.classList.remove('active', 'slide-out', 'slide-back'); }); let container = document.getElementById('customPageContainer'); @@ -1509,7 +1514,15 @@ async function init() { }); // ===== 前进后退 ===== + // popstate 由浏览器前进/后退触发。这类切换用反向滑入动画, + // 与主动点击进入页面区分开,让用户感知到「我是往回走的」。 + let navigatingBack = false; window.addEventListener('popstate', function(e) { + navigatingBack = true; + // 标记只对紧接着的一次切换生效,渲染完成后立即清除, + // 避免影响后续的主动跳转。 + setTimeout(() => { navigatingBack = false; }, 0); + const state = e.state || {}; const page = state.page || 'feed'; const postId = state.postId || null; @@ -1526,6 +1539,9 @@ async function init() { } }); + // 供各页面切换函数查询当前是否为后退导航 + window.isNavigatingBack = () => navigatingBack; + // ===== 私信按钮 ===== const messageBtn = document.getElementById('messageBtn'); if (messageBtn) { diff --git a/js/feed.js b/js/feed.js index 07e396a..a6b3032 100644 --- a/js/feed.js +++ b/js/feed.js @@ -20,8 +20,14 @@ function scrollFeedToTop() { function renderFeed() { const container = document.getElementById('postList'); - container.innerHTML = '
加载中...
'; + // 骨架屏只在首次加载时显示。翻页时列表已有内容,再画一遍骨架会 + // 造成明显的闪动,反而比直接留白更糟。 + const isFirstLoad = container.dataset.loaded !== '1'; + container.innerHTML = isFirstLoad + ? Skeleton.postList(4) + : '
加载中...
'; API.getPosts(currentSort, currentPageNum, PAGE_SIZE).then(result => { + container.dataset.loaded = '1'; const posts = result.data || []; const pagination = result.pagination || { total: 0, totalPages: 1, page: 1 }; totalPages = pagination.totalPages || 1; diff --git a/js/post.js b/js/post.js index bcd4c96..23ad67c 100644 --- a/js/post.js +++ b/js/post.js @@ -16,10 +16,15 @@ function switchToPost(postId) { function showPostPage(postId) { document.getElementById('app').style.display = 'none'; document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out'); + el.classList.remove('active', 'slide-out', 'slide-back'); }); const el = document.getElementById('pagePost'); + // 后退导航时用反向滑入,与主动进入区分 + if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { + el.classList.add('slide-back'); + } el.classList.add('active'); + // 强制重排以重放动画(同一元素连续切换时需要) el.style.animation = 'none'; void el.offsetHeight; el.style.animation = ''; @@ -30,7 +35,7 @@ function showPostPage(postId) { async function renderPostDetail(postId) { const container = document.getElementById('postDetailContent'); const replyArea = document.getElementById('replyArea'); - container.innerHTML = '
加载中...
'; + container.innerHTML = Skeleton.postDetail() + Skeleton.replyItem() + Skeleton.replyItem(); replyArea.style.display = 'none'; try { diff --git a/js/skeleton.js b/js/skeleton.js new file mode 100644 index 0000000..4d5f476 --- /dev/null +++ b/js/skeleton.js @@ -0,0 +1,130 @@ +// ============================================================ +// 💀 骨架屏生成器 +// ------------------------------------------------------------ +// 用与真实内容同构的灰块替代「加载中...」文字,让等待期间就能看出 +// 版面结构,内容出现时也不会发生明显的布局跳动。 +// +// 所有函数返回 HTML 字符串,由调用方写入容器。 +// 数量刻意与首页默认每页条数(20)不同——只画 3~5 条即可, +// 画满 20 条反而增加渲染负担,而用户在下一次绘制前看不完。 +// ============================================================ + +const Skeleton = { + // 单张帖子卡片:头像 + 用户名 + 标题 + 正文两行 + postCard({ withThumbs = false } = {}) { + return ` +
+
+
+
+
+
+
+
+ ${withThumbs ? '
' : ''} +
+ `; + }, + + // 帖子列表 + postList(count = 4) { + let html = ''; + for (let i = 0; i < count; i++) { + // 第一张带缩略图占位,更接近真实列表的参差感 + html += this.postCard({ withThumbs: i === 1 }); + } + return html; + }, + + // 帖子详情:标题 + 多行正文 + 回复区 + postDetail() { + return ` +
+
+
+
+
+
+
+
+
+
+ `; + }, + + // 回复条目 + replyItem() { + return ` +
+
+
+
+
+
+
+ `; + }, + + // 表格类列表(管理后台的用户 / 日志 / 友链) + tableRows(count = 6) { + let html = ''; + for (let i = 0; i < count; i++) { + // 三列宽度固定,模拟表格的列对齐 + html += ` +
+
+
+
+
+ `; + } + return html; + }, + + // 用户主页 + userProfile() { + return ` +
+
+
+
+
+ ${this.postList(2)} + `; + }, +}; + +// 挂到 window 供各模块使用(各文件都是普通 script,共享全局作用域) +window.Skeleton = Skeleton; + +// ============================================================ +// 首屏预置骨架屏 +// ------------------------------------------------------------ +// 真正的列表渲染要等 i18n 初始化完成后才触发(DOMContentLoaded 之后 +// 还有 200ms 延迟)。若不提前占位,用户会先看到一段「加载中...」 +// 文字,等骨架屏出现时数据往往已经回来了,等于白做。 +// +// 这里在脚本执行时(早于 DOMContentLoaded)就把骨架屏写好,让它在 +// 第一帧就可见。renderFeed 拿到数据后会直接覆盖这个容器。 +// ============================================================ +(function primeSkeleton() { + function insert() { + const list = document.getElementById('postList'); + if (list && !list.dataset.loaded) { + list.innerHTML = Skeleton.postList(4); + } + } + if (document.readyState === 'loading') { + // DOM 尚未就绪时,解析到 postList 之后再插入; + // 用 DOMContentLoaded 会偏晚,因此监听 readystatechange 的 + // interactive 阶段。 + document.addEventListener('readystatechange', function onReady() { + if (document.readyState !== 'loading') { + document.removeEventListener('readystatechange', onReady); + insert(); + } + }); + } else { + insert(); + } +})(); diff --git a/js/user.js b/js/user.js index 0aba932..bfef30e 100644 --- a/js/user.js +++ b/js/user.js @@ -4,7 +4,7 @@ async function renderUserProfile(username) { const container = document.getElementById('userProfileContent'); - container.innerHTML = '
加载中...
'; + container.innerHTML = Skeleton.userProfile(); try { // 使用公开接口 /users/profile/:username @@ -80,9 +80,13 @@ async function renderUserProfile(username) { function showUserPage(username) { document.getElementById('app').style.display = 'none'; document.querySelectorAll('.page-slide').forEach(el => { - el.classList.remove('active', 'slide-out'); + el.classList.remove('active', 'slide-out', 'slide-back'); }); const el = document.getElementById('pageUser'); + // 后退导航时用反向滑入,与主动进入区分 + if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) { + el.classList.add('slide-back'); + } el.classList.add('active'); el.style.animation = 'none'; void el.offsetHeight; diff --git a/style.css b/style.css index 98f1ebc..f970c49 100644 --- a/style.css +++ b/style.css @@ -872,6 +872,12 @@ svg { display: block; animation: slideIn 0.35s cubic-bezier(0.22, 1, 0.36, 1); } +/* 后退(浏览器返回键或返回按钮)时用反向动画。 + 原先只有 slideIn 一个方向,前进与后退都从右侧滑入,方向感是错的: + 返回上一页时内容应该从左侧进来。由 JS 在 popstate 上打这个标记。 */ +.page-slide.active.slide-back { + animation: slideInBack 0.35s cubic-bezier(0.22, 1, 0.36, 1); +} @keyframes slideIn { from { transform: translateX(40px); @@ -882,6 +888,16 @@ svg { opacity: 1; } } +@keyframes slideInBack { + from { + transform: translateX(-40px); + opacity: 0.6; + } + to { + transform: translateX(0); + opacity: 1; + } +} .page-slide.slide-out { animation: slideOut 0.25s cubic-bezier(0.55, 0, 1, 0.45) forwards; } @@ -3345,6 +3361,112 @@ select:disabled { /* 搜索框、验证码等内嵌小输入框同样适用,无需单独处理 */ +/* ============================================================ + 💀 骨架屏 + ------------------------------------------------------------ + 替代纯文字的「加载中...」:用与真实内容同构的灰块占位,让用户在 + 等待时就能预判版面结构,也避免内容出现时的布局跳动。 + + 结构由 js/skeleton.js 生成,这里只负责外观与微光动画。 + ============================================================ */ + +.skeleton-card { + background: var(--glass-bg); + border: 1px solid var(--glass-border); + border-radius: var(--radius); + padding: 18px 22px; + margin-bottom: 12px; + /* 轻微模糊,暗示「内容正在成形」而非最终状态 */ + opacity: 0.7; +} + +/* 灰块:用渐变而非纯色,配合扫光动画 */ +.skeleton-line, +.skeleton-avatar, +.skeleton-thumb { + background: linear-gradient( + 90deg, + rgba(127, 127, 147, 0.14) 25%, + rgba(127, 127, 147, 0.24) 37%, + rgba(127, 127, 147, 0.14) 63% + ); + background-size: 400% 100%; + animation: skeletonShimmer 1.4s ease-in-out infinite; + border-radius: 4px; +} + +@keyframes skeletonShimmer { + from { background-position: 100% 50%; } + to { background-position: 0 50%; } +} + +.skeleton-avatar { + width: 32px; + height: 32px; + border-radius: 50%; + flex: none; +} + +.skeleton-line { + height: 12px; + margin-bottom: 8px; +} + +/* 宽度按内容特征设定,避免每行一样长显得机械 */ +.skeleton-line.w-30 { width: 30%; } +.skeleton-line.w-45 { width: 45%; } +.skeleton-line.w-60 { width: 60%; } +.skeleton-line.w-80 { width: 80%; } +.skeleton-line.w-100 { width: 100%; } + +/* 标题行比正文粗 */ +.skeleton-line.skeleton-title { + height: 16px; + margin: 10px 0 12px; +} + +.skeleton-header { + display: flex; + align-items: center; + gap: 10px; + margin-bottom: 4px; +} + +.skeleton-thumb { + width: 120px; + height: 90px; + border-radius: var(--radius-sm); +} + +.skeleton-thumbs { + display: flex; + gap: 6px; + margin-bottom: 10px; +} + +/* 表格骨架(管理后台的用户 / 日志列表) */ +.skeleton-row { + display: flex; + align-items: center; + gap: 16px; + padding: 12px 0; + border-bottom: 1px solid var(--glass-border); +} + +.skeleton-row .skeleton-line { + margin-bottom: 0; +} + +/* 减少动画时停掉扫光,只保留静态灰块 */ +@media (prefers-reduced-motion: reduce) { + .skeleton-line, + .skeleton-avatar, + .skeleton-thumb { + animation: none !important; + background: rgba(127, 127, 147, 0.18); + } +} + /* ============================================================ 🎞️ 滚动驱动动画 ------------------------------------------------------------