This is relatively simple, make a record.
Create a nested node, let the outer node produce a scroll bar, and then use Offsetwidth-clientwidth to get the scroll bar width. Note that to avoid page jitter, you can set the outer elements position:absolute and Visibility:hidden
Reference:
Copy Code code as follows:
function Getscrollwith () {
var wrap = SetAttributes ( Document.createelement (' div '), {
Style: {
width: ' 200px ',
Height: ' 200px ',
Overflow: ' Auto ',
position: ' absolute ',
Visibility: ' Hidden '
}
})
var inner = setattributes (Document.createelement (' Div '), {
Style: {
width: ' 100px ',
Height: ' 2000px '
}
})
Document.body.appendChild (WRA p);
Wrap.appendchild (inner);
var w = wrap.offsetwidth-wrap.clientwidth;
Document.body.removeChild (Wrap);
wrap = null;
inner = null;
Return w;
}
Function SetAttributes (elem,opts) {
for (var key in opts) {
if (typeof opts[key] = = ' string ') {
E Lem[key] = Opts[key];
}else{
if (!elem[key]) {
Elem[key] = {};
}
SetAttributes (Elem[key],opts[key]);
}
}
return elem;
}