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:
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.
The HTML files in the "e2e" folder serve as demos, showcasing how the library can be integrated with different approaches across various applications.
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;`
});
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