fix(feed): 翻页后回到列表顶部
分页控件位于列表末尾(实测在 20 条卡片之后,距页面顶部约 3856px)。 原先点击页码只重新渲染列表、不调整滚动位置,用户停在原处看到的仍是 新一页的底部,需要手动上滚才能读到第一条。 现在翻页后调用 window.scrollTo 回到顶部,并尊重系统的 prefers-reduced-motion 设置:开启时直接跳转,不做平滑滚动。 实测(Puppeteer,视口 1280×900,56 条帖子):先滚至底部 (scrollY=3055)再点第 2 页,翻页后 scrollY=0,新页 20 条正常渲染。 顺带确认了现有分页逻辑本身是完整可用的,无需改动: - 首屏只加载 20 条,请求为 /api/posts?sort=latest&page=1&limit=20 - 56 条数据正确分为 3 页,末页 16 条 - 分页控件含首页、尾页、省略号与上下页,当前页高亮且禁用 - 翻页同步写入 URL(?postpage=N),刷新与分享可保持所在页 - 后端使用 LIMIT/OFFSET,limit 上限 100,page 上限 10000 完整测试套件 25/25 通过。
This commit is contained in:
1 parent
17b6ee35ef
commit
7361ad157d
1 file changed
+15
+15
@@ -6,6 +6,18 @@ let currentSort = 'latest';
|
|||||||
let totalPages = 1;
|
let totalPages = 1;
|
||||||
const PAGE_SIZE = 20;
|
const PAGE_SIZE = 20;
|
||||||
|
|
||||||
|
// 翻页后滚回列表顶部。分页控件在列表末尾,不回到顶部的话用户看到的
|
||||||
|
// 仍是新一页的底部。
|
||||||
|
// 尊重系统的「减少动画」设置:开启时直接跳转,不做平滑滚动。
|
||||||
|
function scrollFeedToTop() {
|
||||||
|
const reduceMotion = window.matchMedia &&
|
||||||
|
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||||||
|
window.scrollTo({
|
||||||
|
top: 0,
|
||||||
|
behavior: reduceMotion ? 'auto' : 'smooth',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function renderFeed() {
|
function renderFeed() {
|
||||||
const container = document.getElementById('postList');
|
const container = document.getElementById('postList');
|
||||||
container.innerHTML = '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
container.innerHTML = '<div style="text-align:center;color:#94a3b8;padding:40px 0;">加载中...</div>';
|
||||||
@@ -160,6 +172,9 @@ function renderFeed() {
|
|||||||
url.searchParams.set('postpage', page);
|
url.searchParams.set('postpage', page);
|
||||||
window.history.pushState({}, '', url);
|
window.history.pushState({}, '', url);
|
||||||
renderFeed();
|
renderFeed();
|
||||||
|
// 分页控件位于列表末尾,翻页后若不回到顶部,用户看到的仍是
|
||||||
|
// 新一页的底部,需要手动上滚才能读到第一条。这里平滑滚回顶部。
|
||||||
|
scrollFeedToTop();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user