A lightweight utility to lock and unlock page scrolling. Prevents background scroll when modals, drawers, or overlays are open, while preserving the user's scroll position.
document.body)npm install @numiko/scroll-lock
import { scrollLock } from '@numiko/scroll-lock';
// Create a scroll lock instance (defaults to document.body)
const lock = scrollLock();
// Disable scrolling
lock.disableScroll();
// Enable scrolling (restores previous scroll position)
lock.enableScroll();
// Check if scrolling is currently locked
lock.getIsLocked(); // true or false
You can also pass a custom element to lock scrolling on:
const customLock = scrollLock(document.querySelector('[data-js-lock-container]'));
See documentation for more detail on how to use the package.