hjkcai-rc-scrollbars
Version:
React scrollbars component
38 lines (37 loc) • 1.07 kB
JavaScript
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');
Object.assign(div.style, {
width: '100px',
height: '100px',
position: 'absolute',
top: '-9999',
overflow: 'scroll',
});
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;
}