25 lines
750 B
JavaScript
25 lines
750 B
JavaScript
|
const scrollbarWidth = () => {
|
||
|
const outer = document.createElement('div');
|
||
|
outer.style.visibility = 'hidden';
|
||
|
outer.style.width = '100px';
|
||
|
document.body.append(outer);
|
||
|
const widthNoScroll = outer.offsetWidth;
|
||
|
outer.style.overflow = 'scroll';
|
||
|
const inner = document.createElement('div');
|
||
|
inner.style.width = '100%';
|
||
|
outer.append(inner);
|
||
|
const widthWithScroll = inner.offsetWidth;
|
||
|
outer.remove();
|
||
|
return widthNoScroll - widthWithScroll;
|
||
|
};
|
||
|
|
||
|
export const getScrollbarWidth = (getWindowScrollbar = true) => {
|
||
|
if (getWindowScrollbar) {
|
||
|
if (document.body.clientHeight > window.innerHeight) {
|
||
|
return scrollbarWidth();
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
return scrollbarWidth();
|
||
|
};
|