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
@@ -459,6 +459,7 @@
|
||||
<script src="js/icons.js"></script>
|
||||
<script src="js/i18n.js"></script>
|
||||
<script src="js/security.js"></script>
|
||||
<script src="js/skeleton.js"></script>
|
||||
<script src="js/api.js"></script>
|
||||
<script src="js/theme.js"></script>
|
||||
<script src="js/captcha.js"></script>
|
||||
|
||||
+3
-3
@@ -73,7 +73,7 @@ let usersSearchKeyword = '';
|
||||
|
||||
function renderAdminUsers(page = 1, search = '') {
|
||||
const container = document.getElementById('adminContent');
|
||||
container.innerHTML = '<div style="text-align:center;color:#94a3b8;padding:20px 0;">加载中...</div>';
|
||||
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 = '<div style="text-align:center;color:#94a3b8;padding:20px 0;">加载中...</div>';
|
||||
|
||||
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 };
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+7
-1
@@ -20,8 +20,14 @@ function scrollFeedToTop() {
|
||||
|
||||
function renderFeed() {
|
||||
const container = document.getElementById('postList');
|
||||
container.innerHTML = '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
||||
// 骨架屏只在首次加载时显示。翻页时列表已有内容,再画一遍骨架会
|
||||
// 造成明显的闪动,反而比直接留白更糟。
|
||||
const isFirstLoad = container.dataset.loaded !== '1';
|
||||
container.innerHTML = isFirstLoad
|
||||
? Skeleton.postList(4)
|
||||
: '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
||||
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;
|
||||
|
||||
+7
-2
@@ -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 = '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
||||
container.innerHTML = Skeleton.postDetail() + Skeleton.replyItem() + Skeleton.replyItem();
|
||||
replyArea.style.display = 'none';
|
||||
|
||||
try {
|
||||
|
||||
+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();
|
||||
}
|
||||
})();
|
||||
+6
-2
@@ -4,7 +4,7 @@
|
||||
|
||||
async function renderUserProfile(username) {
|
||||
const container = document.getElementById('userProfileContent');
|
||||
container.innerHTML = '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
||||
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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
🎞️ 滚动驱动动画
|
||||
------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user