Skip to content

Commit f257138

Browse files
WainWongdataeaseShu
authored andcommitted
fix(chat): prevent auto-scroll from overriding user scroll
When users scroll up to view earlier content in the chat, the auto-scroll timer would keep pulling them back to the bottom. This was caused by the restart condition incorrectly triggering when users were not at the bottom. Changes: - Add userScrolledAway flag to track when user scrolls away from bottom - Stop auto-scroll timer when user scrolls away - Only restart auto-scroll when user manually scrolls back to bottom - Simplify threshold calculation with isNearBottom variable
1 parent 18935b0 commit f257138

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

frontend/src/views/chat/index.vue

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ let scrollTime: any
569569
let scrollingTime: any
570570
let scrollTopVal = 0
571571
let scrolling = false
572+
let userScrolledAway = false // 用户是否主动滚动离开底部
573+
572574
const scrollBottom = () => {
573575
if (scrolling) return
574576
if (!isTyping.value && !getRecommendQuestionsLoading.value) {
@@ -588,22 +590,25 @@ const handleScroll = (val: any) => {
588590
scrollingTime = setTimeout(() => {
589591
scrolling = false
590592
}, 400)
591-
if (
592-
scrollTopVal + 200 <
593-
innerRef.value!.clientHeight - (document.querySelector('.chat-record-list')!.clientHeight - 20)
594-
) {
593+
594+
const threshold =
595+
innerRef.value!.clientHeight -
596+
(document.querySelector('.chat-record-list')!.clientHeight - 20)
597+
const isNearBottom = scrollTopVal + 50 >= threshold
598+
599+
// 用户滚动离开底部时,标记并停止自动滚动
600+
if (!isNearBottom) {
601+
userScrolledAway = true
595602
clearInterval(scrollTime)
596603
scrollTime = null
597604
return
598605
}
599606
600-
if (
601-
!scrollTime &&
602-
isTyping.value &&
603-
scrollTopVal + 30 <
604-
innerRef.value!.clientHeight -
605-
(document.querySelector('.chat-record-list')!.clientHeight - 20)
606-
) {
607+
// 用户滚回底部时,重置标记
608+
userScrolledAway = false
609+
610+
// 只有用户在底部、没有主动滚走、且正在输入时才启动自动滚动
611+
if (!scrollTime && isTyping.value && !userScrolledAway) {
607612
scrollTime = setInterval(() => {
608613
scrollBottom()
609614
}, 300)

0 commit comments

Comments
 (0)