跳到主要內容

useScrollLock

分類
導出大小
1.12 kB
上次變更
5 天前

鎖定元素的滾動。

示範

TopLeft
BottomLeft
TopRight
BottomRight
捲動我
isLocked 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-lock="isLocked">
    <div v-for="item in data" :key="item">
      {{ item }}
    </div>
  </div>
  <button @click="toggleLock()">
    Toggle lock state
  </button>
</template>

類型宣告

typescript
/**
 * Lock scrolling of the element.
 *
 * @see https://vueuse.dev.org.tw/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
Coder Poet
YASS
Doctorwu
Zhaolin Liang
Valery
Dominik Pschenitschni
Robin Scholz
Jelf
wheat

更新日誌

v12.8.0 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 原生型別 (#4636)
v12.3.0 於 2025/1/2
59f75 - feat(toValue): 棄用 @vueuse/shared 中的 toValue,改用 Vue 原生函式
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,優化捆綁包並清理程式碼 (#4349)
v10.10.0 於 2024/5/27
d9ee4 - fix: 解鎖函式無法運作 (#3847)
v10.9.0 於 2024/2/27
74e86 - fix: initialOverflow 無法運作 (#3798)
v10.6.0 於 2023/11/9
03866 - fix(onScrollLock): 快取 el 初始 overflow 值 (#3527)
v10.4.0 於 2023/8/25
8acdb - fix: 支援使用 window 或 document (#3319)
v10.2.0 於 2023/6/16
cdc28 - fix: 修復可滾動子元素檢查 (#3065)
v10.0.0-beta.4 於 2023/4/13
4d757 - feat(types)!: 將 MaybeComputedRef 重新命名為 MaybeRefOrGetter
10e98 - feat(toRef)!: 將 resolveRef 重新命名為 toRef
0a72b - feat(toValue): 將 resolveUnref 重新命名為 toValue
v9.13.0 於 2023/2/18
7e590 - fix: 偵測具有 overflow auto 的父元素 (#2699)

在 MIT 許可證下發布。