近期在接入deepseekR1的深度思考,研究了下deepseek官网的滚动效果,大概如下:用户发出消息后,自动滚动到页面最底部,让最新消息展示在视野中,这时候,我们先处理一次滚动:
const scrollDom = ref(null); // 获取DOM
if (scrollDom.value) {const maxScrollTop = robotScroll.value.scrollHeight - robotScroll.value.clientHeight;scrollDom.value.scrollTop = maxScrollTop;}
这时候,消息发出,AI开始回答,这时候,如果用户没有滚动页面,那么就依然是在底部,并且随着AI回答的文字渐渐冒出,我们去把页面慢慢滚动,保持一直可以看到最新冒出的消息。但是注意需要判断:用户如果滚动了页面(例如在查看顶部其他回答),我们是不能打断用户操作的,这个时候我们不进行页面滚动处理。如下:
// 判断在最底部才继续黏贴在底部滚动
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('未到达底部,不滚动')}});
}
这个时候用户的体验感就比较好了~~