跳到主要内容

useScrollLock

分类
导出大小
1.2 kB
上次更改
8 个月前

锁定元素的滚动。

演示

左上
左下
右上
右下
滚动我
已锁定 false

用法

vue
<script setup lang="ts">
import { 
useScrollLock
} from '@vueuse/core'
import {
useTemplateRef
} from 'vue'
const
el
=
useTemplateRef
<HTMLElement>('el')
const
isLocked
=
useScrollLock
(
el
)
isLocked
.
value
= true // lock
isLocked
.
value
= false // unlock
</script> <template> <
div
ref
="
el
" />
</template>

指令用法

此函数还通过 @vueuse/components 包提供了一个指令版本。了解更多用法

vue
<script setup lang="ts">
import { 
vScrollLock
} from '@vueuse/components'
const
data
=
ref
([1, 2, 3, 4, 5, 6])
const
isLocked
=
ref
(false)
const
toggleLock
= useToggle(
isLocked
)
</script> <template> <
div
v-scroll-lo
ck="
isLocked
">
<
div
v-for="
item
in
data
"
:key
="
item
">
{{
item
}}
</
div
>
</
div
>
<
button
@
click
="
toggleLock
()">
Toggle lock state </
button
>
</template>

类型声明

ts
/**
 * Lock scrolling of the element.
 *
 * @see https://vueuse.org.cn/useScrollLock
 * @param element
 */
export declare function 
useScrollLock
(
element
:
MaybeRefOrGetter
<
HTMLElement | SVGElement | Window | Document | null | undefined >,
initialState
?: boolean,
):
WritableComputedRef
<boolean, boolean>

来源

源代码演示文档

贡献者

Anthony Fu
Anthony Fu
webfansplz
IlyaL
Ayaka Rizumu
IlyaL
Robin
Coder Poet
YASS
Doctorwu
Zhaolin Liang
Valery
Dominik Pschenitschni
Robin Scholz
Jelf
wheat

更新日志

8c521 - feat(components)!: 重构组件并使其保持一致 (#4912)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
d9ee4 - 修复:解锁功能无效 (#3847)
v10.9.0
74e86 - 修复:initialOverflow 不工作 (#3798)
v10.6.0
03866 - 修复(onScrollLock):缓存元素初始溢出值 (#3527)
v10.4.0
8acdb - 修复:支持使用 window 或 document (#3319)
v10.2.0
cdc28 - 修复:修复可滚动子元素检查 (#3065)

根据 MIT 许可证发布。