跳到主要内容

useWindowScroll

分类
导出大小
2.24 kB
上次更改
10 个月前

响应式窗口滚动

演示

在屏幕右下角查看滚动值。
滚动值
x: 0
y: 0

用法

vue
<script setup lang="ts">
import { 
useWindowScroll
} from '@vueuse/core'
const {
x
,
y
} =
useWindowScroll
()
</script> <template> <
div
>
read current x, y scroll: {{
x
}}, {{
y
}}
</
div
>
<
button
@
click
="
x
= 100">
scroll X to 100 </
button
>
<
button
@
click
="
y
= 100">
scroll Y to 100 </
button
>
</template>

类型声明

ts
export interface UseWindowScrollOptions
  extends ConfigurableWindow,
    UseScrollOptions {}
/**
 * Reactive window scroll.
 *
 * @see https://vueuse.org.cn/useWindowScroll
 * @param options
 */
export declare function 
useWindowScroll
(
options
?: UseWindowScrollOptions): {
x
:
WritableComputedRef
<number, number>
y
:
WritableComputedRef
<number, number>
isScrolling
:
ShallowRef
<boolean, boolean>
arrivedState
: {
left
: boolean
right
: boolean
top
: boolean
bottom
: boolean
}
directions
: {
left
: boolean
right
: boolean
top
: boolean
bottom
: boolean
}
measure
(): void
} export type
UseWindowScrollReturn
=
ReturnType
<typeof
useWindowScroll
>

来源

源代码演示文档

贡献者

Anthony Fu
Anthony Fu
Nurettin Kaya
Antério Vieira
青椒肉丝
Robin
Chen
Jelf
webfansplz
Maik Kowol
Shinigami
Alex Kozack

更新日志

v12.3.0
a033e - feat: 内部使用 useScroll (#4424)
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v10.6.0
931b2 - feat: 允许回写到滚动 (#3500)

根据 MIT 许可证发布。