Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useScroll.md
1---2category: Sensors3---45# useScroll67Reactive scroll position and state.89## Usage1011```vue12<script setup lang="ts">13import { useScroll } from '@vueuse/core'14import { useTemplateRef } from 'vue'1516const el = useTemplateRef('el')17const { x, y, isScrolling, arrivedState, directions } = useScroll(el)18</script>1920<template>21<div ref="el" />22</template>23```2425### With offsets2627```ts28import { useScroll } from '@vueuse/core'29// ---cut---30const { x, y, isScrolling, arrivedState, directions } = useScroll(el, {31offset: { top: 30, bottom: 30, right: 30, left: 30 },32})33```3435### Setting scroll position3637Set the `x` and `y` values to make the element scroll to that position.3839```vue40<script setup lang="ts">41import { useScroll } from '@vueuse/core'42import { useTemplateRef } from 'vue'4344const el = useTemplateRef('el')45const { x, y } = useScroll(el)46</script>4748<template>49<div ref="el" />50<button @click="x += 10">51Scroll right 10px52</button>53<button @click="y += 10">54Scroll down 10px55</button>56</template>57```5859### Smooth scrolling6061Set `behavior: smooth` to enable smooth scrolling. The `behavior` option defaults to `auto`, which means no smooth scrolling. See the `behavior` option on [`window.scrollTo()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo) for more information.6263```ts64import { useScroll } from '@vueuse/core'65import { useTemplateRef } from 'vue'6667const el = useTemplateRef('el')68const { x, y } = useScroll(el, { behavior: 'smooth' })6970// Or as a `ref`:71const smooth = ref(false)72const behavior = computed(() => smooth.value ? 'smooth' : 'auto')73const { x, y } = useScroll(el, { behavior })74```7576### Recalculate scroll state7778You can call the `measure()` method to manually update the scroll position and `arrivedState` at any time.7980This is useful, for example, after dynamic content changes or when you want to recalculate the scroll state outside of scroll events.8182```ts83import { useScroll } from '@vueuse/core'84import { nextTick, onMounted, useTemplateRef, watch } from 'vue'8586const el = useTemplateRef('el')87const reactiveValue = shallowRef(false)8889const { measure } = useScroll(el)9091// In a watcher92watch(reactiveValue, () => {93measure()94})9596// Or inside any function97function updateScrollState() {98// ...some logic99nextTick(() => {100measure()101})102}103```104105> [!NOTE]106> it's recommended to call `measure()` inside `nextTick()`, to ensure the DOM is updated first.107> The scroll state is initialized automatically `onMount`.108> You only need to call `measure()` manually if you want to recalculate the state after some dynamic changes.109110## Directive Usage111112```vue113<script setup lang="ts">114import type { UseScrollReturn } from '@vueuse/core'115import { vScroll } from '@vueuse/components'116117const data = ref([1, 2, 3, 4, 5, 6])118119function onScroll(state: UseScrollReturn) {120console.log(state) // {x, y, isScrolling, arrivedState, directions}121}122</script>123124<template>125<div v-scroll="onScroll">126<div v-for="item in data" :key="item">127{{ item }}128</div>129</div>130131<!-- with options -->132<div v-scroll="[onScroll, { throttle: 10 }]">133<div v-for="item in data" :key="item">134{{ item }}135</div>136</div>137</template>138```139140## Type Declarations141142```ts143export interface UseScrollOptions extends ConfigurableWindow {144/**145* Throttle time for scroll event, it’s disabled by default.146*147* @default 0148*/149throttle?: number150/**151* The check time when scrolling ends.152* This configuration will be setting to (throttle + idle) when the `throttle` is configured.153*154* @default 200155*/156idle?: number157/**158* Offset arrived states by x pixels159*160*/161offset?: {162left?: number163right?: number164top?: number165bottom?: number166}167/**168* Use MutationObserver to monitor specific DOM changes,169* such as attribute modifications, child node additions or removals, or subtree changes.170* @default { mutation: boolean }171*/172observe?:173| boolean174| {175mutation?: boolean176}177/**178* Trigger it when scrolling.179*180*/181onScroll?: (e: Event) => void182/**183* Trigger it when scrolling ends.184*185*/186onStop?: (e: Event) => void187/**188* Listener options for scroll event.189*190* @default {capture: false, passive: true}191*/192eventListenerOptions?: boolean | AddEventListenerOptions193/**194* Optionally specify a scroll behavior of `auto` (default, not smooth scrolling) or195* `smooth` (for smooth scrolling) which takes effect when changing the `x` or `y` refs.196*197* @default 'auto'198*/199behavior?: MaybeRefOrGetter<ScrollBehavior>200/**201* On error callback202*203* Default log error to `console.error`204*/205onError?: (error: unknown) => void206}207export interface UseScrollReturn {208x: WritableComputedRef<number>209y: WritableComputedRef<number>210isScrolling: ShallowRef<boolean>211arrivedState: {212left: boolean213right: boolean214top: boolean215bottom: boolean216}217directions: {218left: boolean219right: boolean220top: boolean221bottom: boolean222}223measure: () => void224}225/**226* Reactive scroll.227*228* @see https://vueuse.org/useScroll229* @param element230* @param options231*/232export declare function useScroll(233element: MaybeRefOrGetter<234HTMLElement | SVGElement | Window | Document | null | undefined235>,236options?: UseScrollOptions,237): UseScrollReturn238```239