---
title: useScroll
description: Scroll position, direction and edges of the page or of a scrollable element - show a button after some pixels, hide a header while scrolling down, know when the end of a list is reached.
---
# {{ $frontmatter.title }}
{{ $frontmatter.description }}
## Usage
Scroll the box below: the values follow.
Top
Bottom
```
## Scroll of the page
Without a target, `useScroll` follows the page.
```ts
import { useScroll } from 'maz-ui/composables/useScroll'
import { computed } from 'vue'
const { y, directions } = useScroll()
// Hide the header while scrolling down
const isHeaderHidden = computed(() => y.value > 80 && directions.bottom)
```
::: tip Back to top
For a "back to top" or "scroll to bottom" button, use [MazBtnScroll](./../components/maz-btn-scroll.md): it is built on `useScroll`.
:::
## Reach an edge
`offset` makes `arrivedState` true a bit before the edge. Give one number for every edge, or one per edge.
```ts
const { arrivedState } = useScroll(list, { offset: { bottom: 100 } })
watch(() => arrivedState.bottom, (isAtBottom) => {
if (isAtBottom)
loadMore()
})
```
To load the next page of a list, [useInfiniteScroll](./use-infinite-scroll.md) does it for you.
## Callbacks
```ts
useScroll(undefined, {
onScroll: event => console.log('scroll', event),
onStop: () => console.log('the scroll stopped'),
idle: 300,
})
```
## Behavior
- The target is a ref, a getter, an element, a component, `window` or `document`. The page is the default.
- The values update at most once per frame (`requestAnimationFrame`), with a passive listener.
- `isScrolling` and `directions` go back to `false` after `idle` ms (200 by default) without scroll. `onStop` is called then.
- `scrollTo`, `scrollToTop` and `scrollToBottom` are smooth by default. Pass `'instant'` to jump.
- `measure()` reads the position again, after a change of the content.
- The listener is removed when the component is unmounted, or with `stop()`. On the server, nothing is listened.
## Types
```ts
type ScrollTarget = HTMLElement | Window | Document | ComponentPublicInstance | undefined | null
interface ScrollEdges {
top: boolean
bottom: boolean
left: boolean
right: boolean
}
interface UseScrollOptions {
/**
* Distance in pixels from an edge at which `arrivedState` is already `true`
* @default 0
*/
offset?: number | Partial>
/**
* Time in milliseconds without scroll after which `isScrolling` goes back to `false`
* @default 200
*/
idle?: number
/** Called on each scroll, at most once per frame */
onScroll?: (event: Event) => void
/** Called when the scroll stops, after `idle` */
onStop?: () => void
}
function useScroll(target?: MaybeRefOrGetter, options?: UseScrollOptions): {
x: Readonly>
y: Readonly>
isScrolling: Readonly>
arrivedState: Readonly
directions: Readonly
measure: () => void
scrollTo: (options: { top?: number, left?: number, behavior?: ScrollBehavior }) => void
scrollToTop: (behavior?: ScrollBehavior) => void
scrollToBottom: (behavior?: ScrollBehavior) => void
stop: () => void
}
```