feat: AI生成时智能滚动 - 用户可自由滚动查看,只在底部时自动滚动

This commit is contained in:
2026-04-26 22:48:57 +08:00
parent fb09560259
commit 9098e6b4f7
2 changed files with 37 additions and 5 deletions

View File

@@ -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;
}
});
}
}

View File

@@ -8,12 +8,12 @@
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>AI助手</title>
<link rel="stylesheet" href="style.css?v=2.7.7">
<link rel="stylesheet" href="style.css?v=2.8.0">
<link rel="manifest" href="manifest.json">
</head>
<body>
<div id="app"></div>
<script src="marked.min.js?v=2.7.7"></script>
<script src="app.js?v=2.7.7"></script>
<script src="marked.min.js?v=2.8.0"></script>
<script src="app.js?v=2.8.0"></script>
</body>
</html>