hjkcai-rc-scrollbars
Version:
React scrollbars component
40 lines (39 loc) • 1.11 kB
JavaScript
import css from 'dom-css';
var scrollbarWidth = undefined;
var pxRatio = getPxRatio();
export default function getScrollbarWidth() {
var newPxRatio = getPxRatio();
if (pxRatio !== newPxRatio) {
scrollbarWidth = getScrollbarWidthFromDom();
pxRatio = newPxRatio;
}
if (typeof scrollbarWidth === 'number')
return scrollbarWidth;
if (typeof document !== 'undefined') {
scrollbarWidth = getScrollbarWidthFromDom();
}
else {
scrollbarWidth = 0;
}
return scrollbarWidth || 0;
}
function getScrollbarWidthFromDom() {
var div = document.createElement('div');
css(div, {
width: 100,
height: 100,
position: 'absolute',
top: -9999,
overflow: 'scroll',
MsOverflowStyle: 'scrollbar',
});
document.body.appendChild(div);
var result = div.offsetWidth - div.clientWidth;
document.body.removeChild(div);
return result;
}
function getPxRatio() {
if (typeof window === 'undefined')
return 1;
return window.screen.availWidth / document.documentElement.clientWidth;
}