Skip to content

useScrollLock ​

Category
Export Size
496 B
Last Changed
2 years ago

Lock scrolling of the element.

Demo ​

TopLeft
BottomLeft
TopRight
BottomRight
Scroll Me
isLocked false

Usage ​

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 // lock
isLocked
.
value
= false // unlock
</script> <template> <
div
ref
="
el
" />
</template>

Directive Usage ​

This function also provides a directive version via the @vueuse/components package. Learn more about the usage.

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>

Type Declarations ​

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

Source ​

Source • Demo • Docs

Contributors ​

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

Changelog ​

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)

Released under the MIT License.