diff --git a/www/app.js b/www/app.js index c31c5b5..512a8d5 100644 --- a/www/app.js +++ b/www/app.js @@ -19,6 +19,7 @@ let isLoading = false; // 功能开关 let enableThinking = false; // 深度思考 let enableSearch = false; // 联网搜索 +let autoScrollEnabled = true; // 自动滚动(用户滚动后可关闭) // DOM 元素(初始为 null,在 openConversation 时重新获取) let appContainer = null; @@ -575,6 +576,12 @@ function openConversation(id) { thinkingBtn = document.getElementById('thinkingBtn'); searchBtn = document.getElementById('searchBtn'); + // 重置自动滚动状态 + autoScrollEnabled = true; + + // 设置滚动监听 + setupScrollListener(); + // 绑定按钮事件 const backBtn = document.getElementById('backBtn'); if (backBtn) backBtn.addEventListener('click', showConversationList); @@ -739,6 +746,9 @@ async function sendMessage() { currentConversation.updatedAt = Date.now(); saveConversations(); + // 发送新消息时启用自动滚动 + autoScrollEnabled = true; + renderMessages(); userInput.value = ''; autoResize(userInput); @@ -1298,10 +1308,32 @@ function renderMarkdown(text) { return marked.parse(text); } -// 滚动到底部 +// 滚动到底部(智能滚动:只在用户已在底部时自动滚动) function scrollToBottom() { if (messagesContainer) { - messagesContainer.scrollTop = messagesContainer.scrollHeight; + // 判断用户是否在底部附近(距离底部100px以内) + const isNearBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop - messagesContainer.clientHeight < 100; + + if (isNearBottom || autoScrollEnabled) { + messagesContainer.scrollTop = messagesContainer.scrollHeight; + } + } +} + +// 监听用户滚动行为 +function setupScrollListener() { + if (messagesContainer) { + messagesContainer.addEventListener('scroll', () => { + // 判断用户是否在底部附近 + const isNearBottom = messagesContainer.scrollHeight - messagesContainer.scrollTop - messagesContainer.clientHeight < 100; + + if (isNearBottom) { + autoScrollEnabled = true; + } else { + // 用户往上滚动,停止自动滚动 + autoScrollEnabled = false; + } + }); } } diff --git a/www/index.html b/www/index.html index c3a395e..d0cde40 100644 --- a/www/index.html +++ b/www/index.html @@ -8,12 +8,12 @@ AI助手 - +
- - + + \ No newline at end of file