Detect window scrollbars
Clean JavaScript to detect if the window has a scrollbars.
JavaScript code
function getDocumentHeight() {
return Math.max(
document.body.scrollHeight,
document.body.offsetHeight,
document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight
);
}
function getDocumentWidth() {
return Math.max (
document.body.scrollWidth,
document.body.offsetWidth,
document.documentElement.clientWidth,
document.documentElement.scrollWidth,
document.documentElement.offsetWidth
);
}
function windowHasScrollBarY () {
return (getDocumentHeight() > window.innerHeight);
}
function widnowHasScrollBarX () {
return (getDocumentWidth() > window.innerWidth);
}
if ( windowHasScrollBarY() ) {
// There is a vertical scrollbar
}
if (widnowHasScrollBarX() ) {
// There is a horizontal scrollbar
}