Skip to content

Instantly share code, notes, and snippets.

@faustienf
Created December 3, 2021 08:11
Show Gist options
  • Save faustienf/290d380353a9c1b629976869dfda32f7 to your computer and use it in GitHub Desktop.
Save faustienf/290d380353a9c1b629976869dfda32f7 to your computer and use it in GitHub Desktop.
let scrollWidth: null | number = null;
export const getScrollWidth = (): number => {
if (scrollWidth !== null) {
return scrollWidth;
}
const el = document.createElement('div');
el.style.position = 'fixed';
el.style.zIndex = '-1';
el.style.visibility = 'hidden';
el.style.overflowY = 'scroll';
el.style.width = '100%';
el.style.height = '1px';
document.body.appendChild(el);
scrollWidth = el.offsetWidth - el.clientWidth;
document.body.removeChild(el);
return scrollWidth;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment