feat(ui): 加入骨架屏,并让页面转场区分前进与后退
一、骨架屏 原先 12 处加载态都是同一句「加载中...」文字。改为与真实内容同构的 灰块占位,用户在等待时就能预判版面结构,内容出现时也不会发生明显的 布局跳动。 - 新增 js/skeleton.js,提供 postList / postDetail / replyItem / tableRows / userProfile 五种形态,按内容特征设定灰块宽度, 避免每行等长显得机械 - 已接入帖子列表、帖子详情(含两条回复占位)、用户主页,以及管理 后台的用户列表与日志列表 - 扫光动画(skeletonShimmer)提示「正在加载」而非「内容就是这样」 - 减少动画时停掉扫光,保留静态灰块 一个必须处理的时机问题:真正的列表渲染要等 i18n 初始化完成后才触发 (DOMContentLoaded 之后还有 200ms 延迟)。首版把骨架屏放在 renderFeed 里,实测前 2.8 秒仍是「加载中...」文字,等骨架出现时数据往往已经 回来了,等于白做。现改为在 skeleton.js 脚本执行期(早于 DOMContentLoaded)就写入容器,实测从第一帧起即可见。 翻页时不再显示骨架屏:列表已有内容,重画一遍会造成明显闪动,反而 比直接留白更差。通过容器的 data-loaded 标记区分首次加载与翻页。 二、页面转场区分方向 原先只有 slideIn 一个方向,前进与后退都从右侧滑入——返回上一页时 方向感是错的。现新增 slideInBack(自左侧滑入),在 popstate 上标记 后退导航,由 switchPage 与各 showXxxPage 读取该标记后决定用哪套动画。 标记只在紧接的一次切换生效,渲染完成后立即清除。 实测(Puppeteer,1200ms 延迟 + 150KB/s 限速): - 骨架屏:从首次采样起即为 4 张骨架卡片、16 行占位、4 个头像圆形、 2 个缩略图占位,扫光动画为 skeletonShimmer - 数据返回后骨架卡片残留为 0,正常渲染真实帖子 完整测试套件 25/25 通过。
This commit is contained in:
1 parent
82c878ce33
commit
0ea921186a
8 files changed
+294
-10
No files matched your search
+130
@@ -0,0 +1,130 @@
|
||||
// ============================================================
|
||||
// 💀 骨架屏生成器
|
||||
// ------------------------------------------------------------
|
||||
// 用与真实内容同构的灰块替代「加载中...」文字,让等待期间就能看出
|
||||
// 版面结构,内容出现时也不会发生明显的布局跳动。
|
||||
//
|
||||
// 所有函数返回 HTML 字符串,由调用方写入容器。
|
||||
// 数量刻意与首页默认每页条数(20)不同——只画 3~5 条即可,
|
||||
// 画满 20 条反而增加渲染负担,而用户在下一次绘制前看不完。
|
||||
// ============================================================
|
||||
|
||||
const Skeleton = {
|
||||
// 单张帖子卡片:头像 + 用户名 + 标题 + 正文两行
|
||||
postCard({ withThumbs = false } = {}) {
|
||||
return `
|
||||
<div class="skeleton-card">
|
||||
<div class="skeleton-header">
|
||||
<div class="skeleton-avatar"></div>
|
||||
<div class="skeleton-line w-30" style="margin-bottom:0;"></div>
|
||||
</div>
|
||||
<div class="skeleton-line skeleton-title w-60"></div>
|
||||
<div class="skeleton-line w-100"></div>
|
||||
<div class="skeleton-line w-80"></div>
|
||||
${withThumbs ? '<div class="skeleton-thumbs"><div class="skeleton-thumb"></div><div class="skeleton-thumb"></div></div>' : ''}
|
||||
</div>
|
||||
`;
|
||||
},
|
||||
|
||||
// 帖子列表
|
||||
postList(count = 4) {
|
||||
let html = '';
|
||||
for (let i = 0; i < count; i++) {
|
||||
// 第一张带缩略图占位,更接近真实列表的参差感
|
||||
html += this.postCard({ withThumbs: i === 1 });
|
||||
}
|
||||
return html;
|
||||
},
|
||||
|
||||
// 帖子详情:标题 + 多行正文 + 回复区
|
||||
postDetail() {
|
||||
return `
|
||||
<div class="skeleton-card">
|
||||
<div class="skeleton-header">
|
||||
<div class="skeleton-avatar"></div>
|
||||
<div class="skeleton-line w-30" style="margin-bottom:0;"></div>
|
||||
</div>
|
||||
<div class="skeleton-line skeleton-title w-45"></div>
|
||||
<div class="skeleton-line w-100"></div>
|
||||
<div class="skeleton-line w-100"></div>
|
||||
<div class="skeleton-line w-60"></div>
|
||||
</div>
|
||||
`;
|
||||
},
|
||||
|
||||
// 回复条目
|
||||
replyItem() {
|
||||
return `
|
||||
<div class="skeleton-card" style="padding:14px 18px;">
|
||||
<div class="skeleton-header">
|
||||
<div class="skeleton-avatar" style="width:28px;height:28px;"></div>
|
||||
<div class="skeleton-line w-30" style="margin-bottom:0;"></div>
|
||||
</div>
|
||||
<div class="skeleton-line w-80"></div>
|
||||
</div>
|
||||
`;
|
||||
},
|
||||
|
||||
// 表格类列表(管理后台的用户 / 日志 / 友链)
|
||||
tableRows(count = 6) {
|
||||
let html = '';
|
||||
for (let i = 0; i < count; i++) {
|
||||
// 三列宽度固定,模拟表格的列对齐
|
||||
html += `
|
||||
<div class="skeleton-row">
|
||||
<div class="skeleton-line" style="width:32%;margin-bottom:0;"></div>
|
||||
<div class="skeleton-line" style="width:22%;margin-bottom:0;"></div>
|
||||
<div class="skeleton-line" style="width:16%;margin-bottom:0;"></div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
return html;
|
||||
},
|
||||
|
||||
// 用户主页
|
||||
userProfile() {
|
||||
return `
|
||||
<div class="skeleton-card" style="text-align:center;padding:28px;">
|
||||
<div class="skeleton-avatar" style="width:64px;height:64px;margin:0 auto 14px;"></div>
|
||||
<div class="skeleton-line w-30" style="margin:0 auto 10px;"></div>
|
||||
<div class="skeleton-line w-45" style="margin:0 auto;"></div>
|
||||
</div>
|
||||
${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();
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user