Scroll Spy(useScrollSpy)
Monitor page or container scroll position with throttling and direction detection.
Basic Usage
typescript
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'
const { scrollTop, isScrolling, scrollDirection } = useScrollSpy()API
Options
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
| target | string | No | '' | Scroll container selector (H5 only); falls back to window/page scroll when empty |
| throttle | number | No | 0 | Throttle interval (ms). 0 (default) uses requestAnimationFrame; > 0 uses setTimeout throttling (leading + trailing) |
Returns
| Property | Type | Description |
|---|---|---|
| scrollTop | Ref<number> | Current scroll position (px) |
| isScrolling | Ref<boolean> | Whether scrolling |
| scrollDirection | Ref<'up' | 'down' | 'idle'> | Scroll direction, initial value is 'idle' |
Examples
Monitor Page Scroll
vue
<template>
<view>
<text>Current scroll: {{ scrollTop }}px</text>
<text>Direction: {{ scrollDirection }}</text>
</view>
</template>
<script setup>
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'
const { scrollTop, scrollDirection } = useScrollSpy()
</script>Back to Top Button
vue
<template>
<view v-if="scrollTop > 200" class="back-top" @tap="scrollToTop">
Back to top
</view>
</template>
<script setup>
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'
const { scrollTop } = useScrollSpy()
const scrollToTop = () => {
uni.pageScrollTo({ scrollTop: 0, duration: 300 })
}
</script>Custom Throttle
typescript
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'
// Switch to setTimeout throttling (leading + trailing), at most once every 50ms
const { scrollTop } = useScrollSpy({ throttle: 50 })Monitor Specific Container
vue
<template>
<scroll-view class="container" scroll-y>
<view v-for="i in 100" :key="i">Item {{ i }}</view>
</scroll-view>
</template>
<script setup>
import { useScrollSpy } from '@/uni_modules/see-u-ui/utils/hooks/useScrollSpy'
const { scrollTop } = useScrollSpy({ target: '.container' })
</script>Implementation Details
- H5 platform: registers a
scrolllistener inonMountedand unbinds it inonUnmounted; listens to the element specified bytarget, orwindowwhentargetis empty - Non-H5 platform: uses the
onPageScrollpage-level lifecycle from@dcloudio/uni-app(only effective inside page components) - Defaults to
requestAnimationFramethrottling; whenthrottle > 0, switches to setTimeout throttling (leading + trailing, so the latest value is never dropped) - Direction detection is not affected by throttling — every native event participates in the comparison
- Cleans up event listeners, timers, and
requestAnimationFrameon unmount to prevent memory leaks
