Skip to content

useScroll

类别
导出体积
1.53 kB
上次更改
2 minutes ago

响应式的滚动位置和状态。

示例

左上角
左下角
右上角
右下角
滚动我
X 位置
Y 位置
正在滚动false
到达顶部
true
到达右侧
false
到达底部
false
到达左侧
true
向上滚动
false
向右滚动
false
向下滚动
false
向左滚动
false

用法

vue
<script setup lang="ts">
import { useScroll } from '@vueuse/core'

const el = ref<HTMLElement | null>(null)
const { x, y, isScrolling, arrivedState, directions } = useScroll(el)
</script>

<template>
  <div ref="el" />
</template>

带偏移量

js
const { x, y, isScrolling, arrivedState, directions } = useScroll(el, {
  offset: { top: 30, bottom: 30, right: 30, left: 30 },
})

设置滚动位置

设置 xy 的值以使元素滚动到该位置。

vue
<script setup lang="ts">
import { useScroll } from '@vueuse/core'

const el = ref<HTMLElement | null>(null)
const { x, y } = useScroll(el)
</script>

<template>
  <div ref="el" />
  <button @click="x += 10">
    向右滚动 10px
  </button>
  <button @click="y += 10">
    向下滚动 10px
  </button>
</template>

平滑滚动

设置 behavior: smooth 以启用平滑滚动。behavior 选项默认为 auto,表示没有平滑滚动。有关更多信息,请参阅 window.scrollTo() 上的 behavior 选项。

ts
import { useScroll } from '@vueuse/core'

const el = ref<HTMLElement | null>(null)
const { x, y } = useScroll(el, { behavior: 'smooth' })

// 或作为 `ref`:
const smooth = ref(false)
const behavior = computed(() => smooth.value ? 'smooth' : 'auto')
const { x, y } = useScroll(el, { behavior })
js
import { useScroll } from '@vueuse/core'
const el = ref(null)
const { x, y } = useScroll(el, { behavior: 'smooth' })
// 或作为 `ref`:
const smooth = ref(false)
const behavior = computed(() => (smooth.value ? 'smooth' : 'auto'))
const { x, y } = useScroll(el, { behavior })

指令用法

vue
<script setup lang="ts">
import type { UseScrollReturn } from '@vueuse/core'
import { vScroll } from '@vueuse/components'

const data = ref([1, 2, 3, 4, 5, 6])

function onScroll(state: UseScrollReturn) {
  console.log(state) // {x, y, isScrolling, arrivedState, directions}
}
</script>

<template>
  <div v-scroll="onScroll">
    <div v-for="item in data" :key="item">
      {{ item }}
    </div>
  </div>

  <!-- 带选项 -->
  <div v-scroll="[onScroll, { throttle: 10 }]">
    <div v-for="item in data" :key="item">
      {{ item }}
    </div>
  </div>
</template>

类型声明

显示类型声明
typescript
export interface UseScrollOptions extends ConfigurableWindow {
  /**
   * 滚动事件的节流时间,默认情况下禁用。
   *
   * @default 0
   */
  throttle?: number
  /**
   * 滚动结束时的检查时间。
   * 当配置了 `throttle` 时,该配置将设置为 (throttle + idle)。
   *
   * @default 200
   */
  idle?: number
  /**
   * 以 x 像素偏移到达状态
   *
   */
  offset?: {
    left?: number
    right?: number
    top?: number
    bottom?: number
  }
  /**
   * 滚动时触发。
   *
   */
  onScroll?: (e: Event) => void
  /**
   * 滚动结束时触发。
   *
   */
  onStop?: (e: Event) => void
  /**
   * 滚动事件的监听器选项。
   *
   * @default {capture: false, passive: true}
   */
  eventListenerOptions?: boolean | AddEventListenerOptions
  /**
   * 可选地指定滚动行为为 `auto`(默认,非平滑滚动)或 `smooth`(用于平滑滚动),在更改 `x` 或 `y` 引用时生效。
   *
   * @default 'auto'
   */
  behavior?: MaybeRefOrGetter<ScrollBehavior>
  /**
   * 错误回调
   *
   * 默认将错误记录到 `console.error`
   */
  onError?: (error: unknown) => void
}
/**
 * 响应式滚动。
 *
 * @see https://vueuse.org/useScroll
 * @param element
 * @param options
 */
export declare function useScroll(
  element: MaybeRefOrGetter<
    HTMLElement | SVGElement | Window | Document | null | undefined
  >,
  options?: UseScrollOptions,
): {
  x: WritableComputedRef<number, number>
  y: WritableComputedRef<number, number>
  isScrolling: Ref<boolean, boolean>
  arrivedState: {
    left: boolean
    right: boolean
    top: boolean
    bottom: boolean
  }
  directions: {
    left: boolean
    right: boolean
    top: boolean
    bottom: boolean
  }
  measure(): void
}
export type UseScrollReturn = ReturnType<typeof useScroll>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
webfansplz
Anthony Fu
kongmoumou
Curt Grimes
Dima Kaltovich
Poet
Nico Prat
MinatoHikari
Vladimir
AlanYu
Scott Bedard
Christian Martinez
丶远方
holtergram
Ayaka Rizumu
云游君
btea
Thierry Michel
Pavel Yankovski
Bobakanoosh
Joseph Fitz Hughes

更新日志

v10.10.0 on 5/27/2024
317ca - fix: sync scroll val to internal state, fix #3809 (#3817)
v10.8.0 on 2/20/2024
fab86 - fix: add onError hook and avoid throws by default, fix #3580 (#3605)
v10.6.1 on 11/13/2023
e9742 - fix: can not read properties of null (reading document) (#3544)
v10.6.0 on 11/9/2023
86bd8 - fix: trigger once onMounted to get correct inital arrivedStates values (#3384)
v10.4.0 on 8/25/2023
c1b29 - fix: evade edge case when window or document is Proxy (#3280)
v10.3.0 on 7/30/2023
dde41 - fix: support configurable window (#3229)
v10.2.0 on 6/16/2023
8855f - fix: support window in setArrivedState (#3086)
v10.1.1 on 5/1/2023
a88fe - fix(useInfiniteScroll): re-measure arrived states when async infinite scroll resolves (#3030)
v10.0.0-beta.4 on 4/13/2023
23b9a - fix: add support for row-reverse and column-reverse (#2577)
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v9.13.0 on 2/18/2023
f8872 - feat: support scrollend event (#2716)
v9.5.0 on 11/9/2022
9cd92 - fix: the expected result cannot be returned after setting the throttle parameter (#2390)
v9.3.0 on 9/26/2022
324ff - feat: support setting scroll position and toggling smooth scrolling (#1996)