useScroll
スクロール位置と状態をリアクティブに。
デモ
左上
左下
右上
右下
スクロールしてください
X位置
Y位置
isScrollingfalse
上端に到達
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 },
})
スクロール位置の設定
要素をその位置までスクロールするには、x
および y
の値を設定します。
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">
Scroll right 10px
</button>
<button @click="y += 10">
Scroll down 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' })
// Or as a `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' })
// Or as a `ref`:
const smooth = ref(false)
const behavior = computed(() => (smooth.value ? 'smooth' : 'auto'))
const { x, y } = useScroll(el, { behavior })
ディレクティブの使用法
この関数は、
@vueuse/components
パッケージを介してディレクティブバージョンも提供します。使用法の詳細。
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>
<!-- with options -->
<div v-scroll="[onScroll, { throttle: 10 }]">
<div v-for="item in data" :key="item">
{{ item }}
</div>
</div>
</template>
型宣言
型宣言を表示
typescript
export interface UseScrollOptions extends ConfigurableWindow {
/**
* Throttle time for scroll event, it’s disabled by default.
*
* @default 0
*/
throttle?: number
/**
* The check time when scrolling ends.
* This configuration will be setting to (throttle + idle) when the `throttle` is configured.
*
* @default 200
*/
idle?: number
/**
* Offset arrived states by x pixels
*
*/
offset?: {
left?: number
right?: number
top?: number
bottom?: number
}
/**
* Trigger it when scrolling.
*
*/
onScroll?: (e: Event) => void
/**
* Trigger it when scrolling ends.
*
*/
onStop?: (e: Event) => void
/**
* Listener options for scroll event.
*
* @default {capture: false, passive: true}
*/
eventListenerOptions?: boolean | AddEventListenerOptions
/**
* Optionally specify a scroll behavior of `auto` (default, not smooth scrolling) or
* `smooth` (for smooth scrolling) which takes effect when changing the `x` or `y` refs.
*
* @default 'auto'
*/
behavior?: MaybeRefOrGetter<ScrollBehavior>
/**
* On error callback
*
* Default log error to `console.error`
*/
onError?: (error: unknown) => void
}
/**
* Reactive scroll.
*
* @see https://vueuse.dokyumento.jp/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>
ソース
貢献者
変更履歴
v12.0.0-beta.1
2024/11/21v10.10.0
2024/05/27v10.8.0
2024/02/20v10.6.1
2023/11/13v10.6.0
2023/11/09v10.4.0
2023/08/25v10.3.0
2023/07/30v10.2.0
2023/06/16v10.1.1
2023/05/01v10.0.0-beta.4
2023/04/134d757
- feat(types)!: MaybeComputedRef
を MaybeRefOrGetter
にリネーム0a72b
- feat(toValue): resolveUnref
を toValue
にリネームv9.13.0
2023/02/18v9.5.0
2022/11/09v9.3.0
2022/09/26