当前位置: 首页 > article >正文

vue3大模型流式数据滚动效果hooks

大模型对话,内容区域随着流式数据滚动置底,方法如下:

import type { Ref } from 'vue'
import { nextTick, ref } from 'vue'

type ScrollElement = HTMLDivElement | null

interface ScrollReturn {
  scrollRef: Ref<ScrollElement>
  scrollToBottom: () => Promise<void>
  scrollToTop: () => Promise<void>
  scrollToBottomIfAtBottom: () => Promise<void>
}

export function useScroll(): ScrollReturn {
  const scrollRef = ref<ScrollElement>(null)

  const scrollToBottom = async () => {
    await nextTick()
    if (scrollRef.value) scrollRef.value.scrollTop = scrollRef.value.scrollHeight
  }

  const scrollToTop = async () => {
    await nextTick()
    if (scrollRef.value) scrollRef.value.scrollTop = 0
  }

  const scrollToBottomIfAtBottom = async () => {
    await nextTick()
    if (scrollRef.value) {
      const threshold = 100 // Threshold, indicating the distance threshold to the bottom of the scroll bar.
      const distanceToBottom = scrollRef.value.scrollHeight - scrollRef.value.scrollTop - scrollRef.value.clientHeight
      if (distanceToBottom <= threshold) scrollRef.value.scrollTop = scrollRef.value.scrollHeight
    }
  }

  return {
    scrollRef,
    scrollToBottom,
    scrollToTop,
    scrollToBottomIfAtBottom
  }
}

在vue3中使用:

<div ref="scrollRef" class="chatContent">
	滚动内容
</div>

import { useScroll } from './useScroll'

const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll()

http://www.kler.cn/a/548242.html

相关文章:

  • Linux udp poll函数
  • 自学FOC系列分享--BLDC的电驱
  • GitHub 热点项目介绍
  • 如何判断网页是不是鸿蒙手机浏览器里打开
  • 138,【5】buuctf web [RootersCTF2019]I_<3_Flask
  • 【GPIO】5.理解保护二极管在GPIO过电压保护中的作用
  • qt-C++笔记之QGraphicsScene和 QGraphicsView中setScene、通过scene得到view、通过view得scene
  • python-leetcode-直线上最多的点数
  • 网络安全 “免疫力”:从人体免疫系统看防御策略
  • Python实现AWS Fargate自动化部署系统
  • Vscode 主题,文件图标,插件
  • CSS预处理器——SCSS的灵活语法
  • 导出mysql数据库中表字段信息到excel表中,而不是导出数据
  • LVS-DR集群构建
  • 切换镜像源(npm)
  • TCP的滑动窗口机制及其在流量控制中的作用
  • 编程题-最大子数组和(中等-重点【贪心、动态规划、分治思想的应用】)
  • C++:高度平衡二叉搜索树(AVLTree) [数据结构]
  • 【电脑】u盘重装win7
  • 工作一个月的经历和总结