I have come across a query regarding the calculation of browser scrollbar sizes. However, my focus is on understanding how the solution in the npm package called scrollbar-wdith actually works. Can someone provide an explanation for this?
To give some context, here is the relevant code written in (.coffee):
scrollbarWidth = null
getScrollbarWidth = (recalculate = false) ->
return scrollbarWidth if scrollbarWidth? and not recalculate
return null if document.readyState is 'loading'
div1 = document.createElement 'div'
div2 = document.createElement 'div'
div1.style.width = div2.style.width = div1.style.height = div2.style.height = '100px'
div1.style.overflow = 'scroll'
div2.style.overflow = 'hidden'
document.body.appendChild div1
document.body.appendChild div2
scrollbarWidth = Math.abs div1.scrollHeight - div2.scrollHeight
document.body.removeChild div1
document.body.removeChild div2
scrollbarWidth