AI会话页面滚动处理

56 阅读1分钟
// 判断在最底部才继续黏贴在底部滚动
const stickToBottom = () => {
  const scrollEl = scrollDom.value;
  if (!scrollEl) return;
 
  // 记录当前滚动状态
  const currentScrollTop = scrollEl.scrollTop;
  const currentMaxScroll = scrollEl.scrollHeight - scrollEl.clientHeight;
  
  // 判断是否已经到底部(允许1px误差)
  const isAtBottom = currentScrollTop >= currentMaxScroll - 1;
 
  nextTick(() => {
    if (!scrollEl) return;
    
    // 只有当前在底部时才滚动
    if (isAtBottom) {
       console.log('当前到达底部了,滚动√')
      const newMaxScroll = scrollEl.scrollHeight - scrollEl.clientHeight;
      scrollEl.scrollTop = newMaxScroll;
    } else {
       console.log('未到达底部,不滚动')
    }
  });
}

参考文章