Skip to content

useScrollLock

类别
导出体积
1.22 kB
上次更改
last year

锁定元素的滚动。

示例

左上角
左下角
右上角
右下角
滚动
是否锁定 false

用法

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

指令用法

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
()">
切换锁定状态 </
button
>
</template>

类型声明

ts
/**
 * 锁定元素的滚动。
 *
 * @see https://vueuse.org/useScrollLock
 * @param element
 */
export declare function 
useScrollLock
(
element
:
MaybeRefOrGetter
<
HTMLElement | SVGElement | Window | Document | null | undefined >,
initialState
?: boolean,
):
WritableComputedRef
<boolean, boolean>

源码

源码演示文档

贡献者

ikxin
Anthony Fu
Anthony Fu
webfansplz
IlyaL
Robin
Ayaka Rizumu
一纸忘忧
Jialong Lu
Vida Xie
IlyaL
Coder Poet
YASS
Doctorwu
Zhaolin Liang
Valery
Dominik Pschenitschni
Robin Scholz
Jelf
wheat

更新日志

52d68 - fix(directive): create disposable directive func cleanup of side effects unmounted (#5244)
8c521 - feat(components)!: refactor components and make them consistent (#4912)
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
d9ee4 - fix: function unlock does not work (#3847)
74e86 - fix: initialOverflow is not working (#3798)