Skip to content

useScroll 滚动钩子

用于监听滚动事件并在滚动达到特定阈值时触发回调的 Vue 钩子函数。

基本用法

ts
import { ref } from 'vue'
import { useScroll } from '@minilo/utils'

const { scrollPosition, isReached, checkThreshold, updateThreshold } = useScroll({
  container: window,
  initialThreshold: 100,
  onReach: (reached) => {
    console.log('滚动阈值到达状态:', reached)
  },
  direction: 'vertical',
  throttleTime: 200
})

参数

参数说明类型默认值必填
container滚动容器string | HTMLElement | Windowwindow
initialThreshold初始阈值number0
onReach达到阈值时的回调函数Function | nullnull
direction滚动方向'vertical' | 'horizontal''vertical'
throttleTime节流时间(毫秒)number200

返回值

属性说明类型
scrollPosition当前滚动位置Ref<number>
isReached是否达到阈值Ref<boolean>
checkThreshold手动检查阈值的方法() => void
updateThreshold更新阈值的方法(val: number) => void