589 B
589 B
To get the scrollbar width, we can create an element with the scroll, but without borders and paddings.
Then the difference between its full width offsetWidth
and the inner content area width clientWidth
will be exactly the scrollbar:
// create a div with the scroll
let div = document.createElement('div');
div.style.overflowY = 'scroll';
div.style.width = '50px';
div.style.height = '50px';
// must put it in the document, otherwise sizes will be 0
document.body.append(div);
let scrollWidth = div.offsetWidth - div.clientWidth;
div.remove();
alert(scrollWidth);