Skip to content

Instantly share code, notes, and snippets.

@tsmd
Last active November 20, 2018 08:10
Show Gist options
  • Save tsmd/d1f08823eb5ee8435b064221c1c5caf7 to your computer and use it in GitHub Desktop.
Save tsmd/d1f08823eb5ee8435b064221c1c5caf7 to your computer and use it in GitHub Desktop.
let scrollbarWidth;
export default function measureScrollBarWidth(opts = {}) {
const { force = false } = opts;
if (force || typeof scrollbarWidth === "undefined") {
const div = document.createElement("div");
div.style.position = "absolute";
div.style.overflowY = "scroll";
div.style.width = div.style.height = "100px";
document.body.appendChild(div);
scrollbarWidth = div.offsetWidth - div.scrollWidth;
document.body.removeChild(div);
}
return scrollbarWidth;
}
import measureScrollBarWidth from "./measure-scroll-bar-width";
export default function setScrollbarCssVar() {
const scrollBarWidth = measureScrollBarWidth();
document.documentElement.style.setProperty("--scrollbar-width", `${scrollBarWidth}px`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment