fix(chat): 上翻历史时不再被自动跟随拽回底部

#chat-view 的 scroll 事件原来无条件用 userPaused = !isNearBottom() 收敛状态,而
isNearBottom() 留了 80px 容差(那是给内容自身增长的余量:思考文字每帧变长,
scrollTop 没动、距底也会变大)。用户往上滚不到 80px 就被判成「还在底部」,下一帧
maybeAutoScroll() 又 scrollBottom() 拽回去——思考阶段每帧都在拉,表现就是
「滚不动、一直在抖」。

加一个 userPinned 记号:滚轮上翻 / 手指下拖时置位,之后 scroll 事件只认「真的滚回
最底部(≤2px)」才解锁,不再被 80px 容差覆盖。点右下角回底按钮、切会话要跟底、
后台补拉尾部消息且原来就在底部,这三处一并清零。
This commit is contained in:
2026-11-23 07:57:21 +08:00
parent 64a2b3ff1a
commit f55df51088
+20 -7
View File
@@ -433,7 +433,7 @@
const atBottom = isNearBottom(); const atBottom = isNearBottom();
appendMessagesDom(tail, box).closeArc(); appendMessagesDom(tail, box).closeArc();
state.chatLoadedCount += tail.length; state.chatLoadedCount += tail.length;
if (atBottom) { userPaused = false; scrollBottom(); } if (atBottom) { userPaused = false; userPinned = false; scrollBottom(); }
} }
updateScrollBottomBtn(); updateScrollBottomBtn();
return true; return true;
@@ -479,6 +479,7 @@
hookToolCardLazyLoad(); hookToolCardLazyLoad();
if (followBottom) { if (followBottom) {
userPaused = false; userPaused = false;
userPinned = false;
scrollBottom(); scrollBottom();
} else { } else {
userPaused = true; // 用户原本在翻历史:保持暂停跟随与原阅读位置 userPaused = true; // 用户原本在翻历史:保持暂停跟随与原阅读位置
@@ -591,6 +592,10 @@
// 把内容推过阈值后 isNearBottom 会变 false,导致后续跟着丢失,看起来像「不跟了」。 // 把内容推过阈值后 isNearBottom 会变 false,导致后续跟着丢失,看起来像「不跟了」。
// 「是否贴底」在 scroll 事件里捕获就行,那一刻是 DOM 变更前的真实位置。 // 「是否贴底」在 scroll 事件里捕获就行,那一刻是 DOM 变更前的真实位置。
let userPaused = false; let userPaused = false;
// 用户主动上翻后「钉住」:此时不再用 80px 容差判断(那是给内容自身增长留的余量),
// 只看是否真的滚回最底部。否则上翻不到 80px 就被 scroll 事件判成「还在底部」,
// 下一帧 maybeAutoScroll 又把视口拽回去——表现为「滚不动、一直在抖」。
let userPinned = false;
function maybeAutoScroll() { function maybeAutoScroll() {
if (!userPaused) scrollBottom(); if (!userPaused) scrollBottom();
} }
@@ -611,6 +616,7 @@
} }
function clickScrollBottom() { function clickScrollBottom() {
userPaused = false; userPaused = false;
userPinned = false;
scrollBottom(); scrollBottom();
updateScrollBottomBtn(); updateScrollBottomBtn();
} }
@@ -620,18 +626,25 @@
function bindChatScroll() { function bindChatScroll() {
const box = $('chat-view'); const box = $('chat-view');
let touchY = 0; let touchY = 0;
box.addEventListener('wheel', e => { if (e.deltaY < 0) userPaused = true; }, { passive: true }); const pin = () => { userPaused = true; userPinned = true; };
box.addEventListener('wheel', e => { if (e.deltaY < 0) pin(); }, { passive: true });
box.addEventListener('touchstart', e => { touchY = e.touches[0].clientY; }, { passive: true }); box.addEventListener('touchstart', e => { touchY = e.touches[0].clientY; }, { passive: true });
box.addEventListener('touchmove', e => { box.addEventListener('touchmove', e => {
const dy = e.touches[0].clientY - touchY; const dy = e.touches[0].clientY - touchY;
if (dy > 3) userPaused = true; if (dy > 3) pin();
touchY = e.touches[0].clientY; touchY = e.touches[0].clientY;
}, { passive: true }); }, { passive: true });
box.addEventListener('scroll', () => { box.addEventListener('scroll', () => {
// 用 scroll 事件(而非 wheel/touchmove)统一收敛「是否在底部」, if (userPinned) {
// 这样拖滚动条、键盘 PageUp/Home、触屏惯性滑等都会被纳入判断。 // 钉住了:只认「真的到底」,否则保持暂停,不再被 80px 容差覆盖
// 程序触发的 scrollBottom 总是精确落底,isNearBottom() 恒为 true,不会误标记。 if (box.scrollHeight - box.scrollTop - box.clientHeight <= 2) { userPinned = false; userPaused = false; }
userPaused = !isNearBottom(); else userPaused = true;
} else {
// 没钉:用 scroll 事件(而非 wheel/touchmove)统一收敛「是否在底部」,
// 这样拖滚动条、键盘 PageUp/Home、触屏惯性滑等都会被纳入判断。
// 程序触发的 scrollBottom 总是精确落底,isNearBottom() 恒为 true,不会误标记。
userPaused = !isNearBottom();
}
updateScrollBottomBtn(); updateScrollBottomBtn();
if (box.scrollTop <= CHAT_TOP_TRIGGER) loadOlderMessages(); // 上滑接近顶部 → 懒加载更早历史 if (box.scrollTop <= CHAT_TOP_TRIGGER) loadOlderMessages(); // 上滑接近顶部 → 懒加载更早历史
}, { passive: true }); }, { passive: true });