npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

scroll-padlock

v3.0.0

Published

Locks elements scroll handling scrollbar gaps and iOS Safari with CSS variables

Readme

ScrollPadlock

Node.js CI scroll-padlock (latest) scroll-padlock (downloads) license

A small unobtrusive script aimed to encourage a CSS-first approach when locking HTML elements scroll. It is entirely written in vanilla JavaScript with no dependencies.

Without scrollbar gap compensation:

without scrollbar gap compensation

With scrollbar gap compensation:

with scrollbar gap compensation

The library exports a setStyle function that appends CSS styles targeting the default .scroll-padlock selector. By default, it uses the page's scrolling element and the window object to retrieve values, which are then assigned to CSS variables for use as preferred.

Examples

The HTML files in the "e2e" folder serve as demos, showcasing how the library can be integrated with different approaches across various applications.

Usage

It can be installed via npm:

npm install scroll-padlock
import { setStyle } from "scroll-padlock";

setStyle();

It can be imported as an ES module in the browser:

<script type="importmap">
  {
    "imports": {
      "scroll-padlock": "https://cdn.jsdelivr.net/npm/scroll-padlock@latest/+esm"
    }
  }
</script>

<script type="module">
  import { setStyle } from "scroll-padlock";

  setStyle();
</script>

It can be used globally by including the UMD version of the script:

<script src="https://cdn.jsdelivr.net/npm/scroll-padlock@latest/dist/scroll-padlock.umd.min.js"></script>

<script>
  window.scrollPadlock.setStyle();
</script>

After calling setStyle, the following CSS variables become available:

  • --scroll-top: the number of pixels the element's content is scrolled vertically.
  • --scroll-left: the number of pixels the element's content is scrolled horizontally.
  • --scroll-width: the total width of the element's scrollable content, including the non-visible part.
  • --scroll-height: the total height of the element's scrollable content, including the non-visible part.
  • --scrollbar-width: the width of the vertical scrollbar of the element.
  • --scrollbar-height: the height of the horizontal scrollbar of the element.
  • --offset-width: the total visible width of the element, including the scrollbar.
  • --offset-height: the total visible height of the element, including the scrollbar.
  • --client-width: the visible width of the element, excluding the scrollbar.
  • --client-height: the visible height of the element, excluding the scrollbar.

These CSS variables can be used to implement the preferred approach to prevent the element scroll or to add the scrollbar gap componsation, see the following basic example:

.scroll-padlock {
  overflow: hidden;

  padding-right: var(--scrollbar-width);
}

Since each function call updates the CSS variables, a good time to call it is immediately before adding the CSS class that would lock the element scroll.

setStyle();

document.scrollingElement.classList.add('scroll-padlock');

The setStyle function accepts an options object which customizes its behavior. Here are the available options:

  • element: the DOM element that will be used to retrieve the CSS variables values.
  • selector: the CSS selector string that identifies the target element.
  • formatter: a function that allows to customize the the CSS styles to be added.
setStyle({
  element: document.querySelector('#custom-scrolling-element'),

  selector: '.custom-element-scroll-padlock',

  formatter: ({ clientWidth }) => `--width-without-scrollbar: ${clientWidth}px;`
});

Contributing

Node version 20.11.0 or higher is required in order to execute the tests.

npm test

To generate the unit tests coverage in a readable format, lcov can be used.

genhtml --branch-coverage lcov.info -o coverage

The locally built library is imported in the end-to-end tests, so a build is required.

npm run build

To run the end-to-end tests, use the following command:

npm run test:e2e