body-scroll-lock vs scroll-lock
Scroll Locking Libraries Comparison
1 Year
body-scroll-lockscroll-lockSimilar Packages:
What's Scroll Locking Libraries?

Scroll locking libraries are designed to manage the scrolling behavior of web applications, particularly when modal dialogs or overlays are displayed. They prevent background content from scrolling while a modal is active, enhancing user experience by keeping focus on the modal content. These libraries provide various methods to lock and unlock scrolling, ensuring that the user interface remains intuitive and user-friendly during interactions with overlays or pop-ups.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
body-scroll-lock940,6764,091-1214 years agoMIT
scroll-lock44,207274-44 years agoMIT
Feature Comparison: body-scroll-lock vs scroll-lock

Targeting

  • body-scroll-lock:

    body-scroll-lock is specifically designed to lock scrolling on the body element of the page. This makes it ideal for scenarios where you want to prevent the entire page from scrolling when a modal or overlay is displayed, ensuring that users focus solely on the modal content without distractions.

  • scroll-lock:

    scroll-lock allows for locking scroll on any specified element, not limited to the body. This flexibility is beneficial for applications with multiple scrollable areas, enabling developers to control scroll behavior on specific containers or elements as needed.

Ease of Use

  • body-scroll-lock:

    body-scroll-lock offers a simple and intuitive API, making it easy to implement scroll locking with minimal configuration. Developers can quickly enable or disable scroll locking with just a few lines of code, making it a great choice for straightforward use cases.

  • scroll-lock:

    scroll-lock provides a more complex API that may require additional setup and understanding. While it offers more features, the learning curve can be steeper for developers who are new to managing scroll behavior, necessitating a bit more effort to implement effectively.

Performance

  • body-scroll-lock:

    body-scroll-lock is optimized for performance, ensuring that it does not introduce significant overhead when locking scroll. It is lightweight and focuses solely on the body element, which helps maintain smooth interactions without impacting the overall performance of the application.

  • scroll-lock:

    scroll-lock may introduce more complexity in terms of performance, especially when managing multiple scrollable elements. While it is designed to be efficient, developers must be mindful of how they implement it to avoid potential performance bottlenecks in applications with many scrollable areas.

Compatibility

  • body-scroll-lock:

    body-scroll-lock is compatible with most modern browsers and does not rely on any external dependencies, making it a reliable choice for projects that require straightforward scroll locking without additional overhead.

  • scroll-lock:

    scroll-lock also boasts broad compatibility with modern browsers, but it may require additional handling for specific edge cases, especially when dealing with nested scrollable elements. Developers should test thoroughly to ensure consistent behavior across different environments.

Use Cases

  • body-scroll-lock:

    body-scroll-lock is ideal for basic modal implementations where the primary requirement is to prevent background scrolling. It is particularly useful for simple applications or websites that need a quick solution for modals or overlays.

  • scroll-lock:

    scroll-lock is better suited for complex applications that involve multiple modals or scrollable areas. It is particularly beneficial in scenarios where different sections of the application need to maintain their own scroll behavior while others are locked.

How to Choose: body-scroll-lock vs scroll-lock
  • body-scroll-lock:

    Choose body-scroll-lock if you need a lightweight solution that specifically targets the body element for scroll locking. It is particularly useful for scenarios where you want to prevent background scrolling while a modal is open, and it offers a straightforward API for enabling and disabling scroll lock.

  • scroll-lock:

    Choose scroll-lock if you require a more comprehensive solution that can handle scroll locking for multiple elements, not just the body. It provides additional features such as the ability to lock scrolling on specific containers and manage scroll behavior more granularly, making it suitable for complex layouts.

README for body-scroll-lock

Body scroll lock...just works with everything ;-)

Why BSL?

Enables body scroll locking (for iOS Mobile and Tablet, Android, desktop Safari/Chrome/Firefox) without breaking scrolling of a target element (eg. modal/lightbox/flyouts/nav-menus).

Features:

  • disables body scroll WITHOUT disabling scroll of a target element
  • works on iOS mobile/tablet (!!)
  • works on Android
  • works on Safari desktop
  • works on Chrome/Firefox
  • works with vanilla JS and frameworks such as React / Angular / VueJS
  • supports nested target elements (eg. a modal that appears on top of a flyout)
  • can reserve scrollbar width
  • -webkit-overflow-scrolling: touch still works

Aren't the alternative approaches sufficient?

  • the approach document.body.ontouchmove = (e) => { e.preventDefault(); return false; }; locks the body scroll, but ALSO locks the scroll of a target element (eg. modal).
  • the approach overflow: hidden on the body or html elements doesn't work for all browsers
  • the position: fixed approach causes the body scroll to reset
  • some approaches break inertia/momentum/rubber-band scrolling on iOS

LIGHT Package Size:

minzip size

Install

$ yarn add body-scroll-lock

or

$ npm install body-scroll-lock

You can also load via a <script src="lib/bodyScrollLock.js"></script> tag (refer to the lib folder).

Usage examples

Common JS
// 1. Import the functions
const bodyScrollLock = require('body-scroll-lock');
const disableBodyScroll = bodyScrollLock.disableBodyScroll;
const enableBodyScroll = bodyScrollLock.enableBodyScroll;

// 2. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
// Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
// This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
const targetElement = document.querySelector('#someElementId');

// 3. ...in some event handler after showing the target element...disable body scroll
disableBodyScroll(targetElement);

// 4. ...in some event handler after hiding the target element...
enableBodyScroll(targetElement);
React/ES6
// 1. Import the functions
import { disableBodyScroll, enableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock';

class SomeComponent extends React.Component {
  targetElement = null;

  componentDidMount() {
    // 2. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
    // Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
    // This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
    this.targetElement = document.querySelector('#targetElementId');
  }

  showTargetElement = () => {
    // ... some logic to show target element

    // 3. Disable body scroll
    disableBodyScroll(this.targetElement);
  };

  hideTargetElement = () => {
    // ... some logic to hide target element

    // 4. Re-enable body scroll
    enableBodyScroll(this.targetElement);
  };

  componentWillUnmount() {
    // 5. Useful if we have called disableBodyScroll for multiple target elements,
    // and we just want a kill-switch to undo all that.
    // OR useful for if the `hideTargetElement()` function got circumvented eg. visitor
    // clicks a link which takes him/her to a different page within the app.
    clearAllBodyScrollLocks();
  }

  render() {
    return <div>some JSX to go here</div>;
  }
}
React/ES6 with Refs
// 1. Import the functions
import { disableBodyScroll, enableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock';

class SomeComponent extends React.Component {
  // 2. Initialise your ref and targetElement here
  targetRef = React.createRef();
  targetElement = null;

  componentDidMount() {
    // 3. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
    // Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
    // This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
    this.targetElement = this.targetRef.current;
  }

  showTargetElement = () => {
    // ... some logic to show target element

    // 4. Disable body scroll
    disableBodyScroll(this.targetElement);
  };

  hideTargetElement = () => {
    // ... some logic to hide target element

    // 5. Re-enable body scroll
    enableBodyScroll(this.targetElement);
  };

  componentWillUnmount() {
    // 5. Useful if we have called disableBodyScroll for multiple target elements,
    // and we just want a kill-switch to undo all that.
    // OR useful for if the `hideTargetElement()` function got circumvented eg. visitor
    // clicks a link which takes him/her to a different page within the app.
    clearAllBodyScrollLocks();
  }

  render() {
    return (
      // 6. Pass your ref with the reference to the targetElement to SomeOtherComponent
      <SomeOtherComponent ref={this.targetRef}>some JSX to go here</SomeOtherComponent>
    );
  }
}

// 7. SomeOtherComponent needs to be a Class component to receive the ref (unless Hooks - https://reactjs.org/docs/hooks-faq.html#can-i-make-a-ref-to-a-function-component - are used).
class SomeOtherComponent extends React.Component {
  componentDidMount() {
    // Your logic on mount goes here
  }

  // 8. BSL will be applied to div below in SomeOtherComponent and persist scrolling for the container
  render() {
    return <div>some JSX to go here</div>;
  }
}
Angular
import { Component, ElementRef, OnDestroy, ViewChild } from "@angular/core";

// 1. Import the functions
import {
  disableBodyScroll,
  enableBodyScroll,
  clearAllBodyScrollLocks
} from "body-scroll-lock";

@Component({
  selector: "app-scroll-block",
  templateUrl: "./scroll-block.component.html",
  styleUrls: ["./scroll-block.component.css"]
})
export class SomeComponent implements OnDestroy {
  // 2. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
  // Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
  // This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
  @ViewChild("scrollTarget") scrollTarget: ElementRef;

  showTargetElement() {
    // ... some logic to show target element

    // 3. Disable body scroll
    disableBodyScroll(this.scrollTarget.nativeElement);
  }
  
  hideTargetElement() {
    // ... some logic to hide target element

    // 4. Re-enable body scroll
    enableBodyScroll(this.scrollTarget.nativeElement);
  }

  ngOnDestroy() {
    // 5. Useful if we have called disableBodyScroll for multiple target elements,
    // and we just want a kill-switch to undo all that.
    // OR useful for if the `hideTargetElement()` function got circumvented eg. visitor
    // clicks a link which takes him/her to a different page within the app.
    clearAllBodyScrollLocks();
  }
}

Vanilla JS

In the html:

<head>
  <script src="some-path-where-you-dump-the-javascript-libraries/lib/bodyScrollLock.js"></script>
</head>

Then in the javascript:

// 1. Get a target element that you want to persist scrolling for (such as a modal/lightbox/flyout/nav).
// Specifically, the target element is the one we would like to allow scroll on (NOT a parent of that element).
// This is also the element to apply the CSS '-webkit-overflow-scrolling: touch;' if desired.
const targetElement = document.querySelector('#someElementId');

// 2. ...in some event handler after showing the target element...disable body scroll
bodyScrollLock.disableBodyScroll(targetElement);

// 3. ...in some event handler after hiding the target element...
bodyScrollLock.enableBodyScroll(targetElement);

// 4. Useful if we have called disableBodyScroll for multiple target elements,
// and we just want a kill-switch to undo all that.
bodyScrollLock.clearAllBodyScrollLocks();

Demo

Check out the demo, powered by Vercel.

  • https://bodyscrolllock.vercel.app for a basic example
  • https://bodyscrolllock-modal.vercel.app for an example with a modal.

Functions

| Function | Arguments | Return | Description | | :------------------------ | :------------------------------------------------------------- | :----: | :----------------------------------------------------------- | | disableBodyScroll | targetElement: HTMLElement
options: BodyScrollOptions | void | Disables body scroll while enabling scroll on target element | | enableBodyScroll | targetElement: HTMLElement | void | Enables body scroll and removing listeners on target element | | clearAllBodyScrollLocks | null | void | Clears all scroll locks |

Options

reserveScrollBarGap

optional, default: false

If the overflow property of the body is set to hidden, the body widens by the width of the scrollbar. This produces an unpleasant flickering effect, especially on websites with centered content. If the reserveScrollBarGap option is set, this gap is filled by a padding-right on the body element. If disableBodyScroll is called for the last target element, or clearAllBodyScrollLocks is called, the padding-right is automatically reset to the previous value.

import { disableBodyScroll } from 'body-scroll-lock';
import type { BodyScrollOptions } from 'body-scroll-lock';

const options: BodyScrollOptions = {
  reserveScrollBarGap: true,
};

disableBodyScroll(targetElement, options);

allowTouchMove

optional, default: undefined

To disable scrolling on iOS, disableBodyScroll prevents touchmove events. However, there are cases where you have called disableBodyScroll on an element, but its children still require touchmove events to function.

See below for 2 use cases:

Simple
disableBodyScroll(container, {
  allowTouchMove: el => el.tagName === 'TEXTAREA',
});
More Complex

Javascript:

disableBodyScroll(container, {
  allowTouchMove: el => {
    while (el && el !== document.body) {
      if (el.getAttribute('body-scroll-lock-ignore') !== null) {
        return true;
      }

      el = el.parentElement;
    }
  },
});

Html:

<div id="container">
  <div id="scrolling-map" body-scroll-lock-ignore>
    ...
  </div>
</div>

References

https://medium.com/jsdownunder/locking-body-scroll-for-all-devices-22def9615177 https://stackoverflow.com/questions/41594997/ios-10-safari-prevent-scrolling-behind-a-fixed-overlay-and-maintain-scroll-posi

Changelog

Refer to the releases page.