@@ -291,6 +291,10 @@ async function renderMessage(role, content, index) {
291291 setupEditButtons ( ) ;
292292 }
293293 //chat.scrollTop = chat.scrollHeight;
294+
295+ if ( autoScrollEnabled ) {
296+ smartScroll ( ) ;
297+ }
294298}
295299
296300// 消息处理
@@ -367,6 +371,65 @@ function handleKeyDown(e) {
367371 }
368372}
369373
374+
375+ // 优化版智能滚动控制
376+ // 配置常量
377+ const SCROLL_THRESHOLD = 50 ; // 距离底部50px视为触底
378+ let autoScrollEnabled = false ;
379+ let lastScrollTop = 0 ;
380+
381+ // 精准的滚轮方向检测
382+ function handleWheel ( e ) {
383+ const isScrollingDown = e . deltaY > 0 ;
384+ checkScrollIntent ( isScrollingDown ) ;
385+ }
386+
387+ // 滚动意图检测
388+ function checkScrollIntent ( isScrollingDown ) {
389+ const currentPos = chat . scrollTop + chat . clientHeight ;
390+ const maxPos = chat . scrollHeight ;
391+ const distanceToBottom = maxPos - currentPos ;
392+
393+ // 判断条件
394+ if ( isScrollingDown ) {
395+ // 向下滚动时:距离底部小于阈值则开启自动滚动
396+ autoScrollEnabled = distanceToBottom <= SCROLL_THRESHOLD ;
397+ } else {
398+ // 任何向上滚动动作立即关闭自动滚动
399+ autoScrollEnabled = false ;
400+ }
401+
402+ // 调试输出
403+ console . log ( `方向: ${ isScrollingDown ? '↓' : '↑' } | 距底部: ${ distanceToBottom } px | 自动: ${ autoScrollEnabled } ` ) ;
404+ }
405+
406+ // 智能滚动执行
407+ function smartScroll ( ) {
408+ if ( autoScrollEnabled ) {
409+ chat . scrollTop = chat . scrollHeight ;
410+ }
411+ }
412+
413+ // 初始化
414+ function setupScroll ( ) {
415+ // 监听滚轮事件
416+ chat . addEventListener ( 'wheel' , handleWheel , { passive : true } ) ;
417+
418+ // 实时滚动检测(使用requestAnimationFrame优化性能)
419+ let lastRender = 0 ;
420+ const checkScroll = ( timestamp ) => {
421+ if ( timestamp - lastRender > 100 ) { // 每100ms检查一次
422+ if ( autoScrollEnabled ) {
423+ smartScroll ( ) ;
424+ }
425+ lastRender = timestamp ;
426+ }
427+ requestAnimationFrame ( checkScroll ) ;
428+ } ;
429+ requestAnimationFrame ( checkScroll ) ;
430+ }
431+
432+
370433// 主初始化函数
371434function initializeWebview ( ) {
372435 // 初始化库和事件监听
@@ -375,7 +438,8 @@ function initializeWebview() {
375438 setupInputHandlers ( ) ;
376439 setupEditButtons ( ) ;
377440 setupCopyButtonDelegation ( ) ;
441+
442+ setupScroll ( ) ;
378443}
379444
380445initializeWebview ( ) ;
381-
0 commit comments