function getScrollbarWidth() {
return window.innerWidth - document.documentElement.clientWidth;
}<button style="margin-top: 0" onclick="alert('Scroll bar width is ' + (window.innerWidth - document.documentElement.clientWidth) + 'px')">Получить ширину полосы скроллбара!</button>Для div:
((window.innerWidth - document.getElementById('div').clientWidth) + 'px')I've recently been working on an advanced JavaScript-based grid solution and let me tell you: it's quite an undertaking. Making sure the grid is accessible, reactive, efficient, and cross-browser compatible is difficult, but even the minor workings of each of those are hard. One small task was detecting the width of the vertical scrollbar so that I know how wide the grid body truly was. Here's the tiny snippet to do it:
The CSS
The element we create for measurement will need to be positioned off screen so the user doesn't notice it:
/* way the hell off screen */
.scrollbar-measure {
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
}You could add these styles directly to the element, but they'd bloat the JavaScript portion a bit.
The JavaScript
The obvious parts is creating a DIV to inject into the DOM and adding the CSS class we created above:
// Create the measurement node
var scrollDiv = document.createElement("div");
scrollDiv.className = "scrollbar-measure";
document.body.appendChild(scrollDiv);
// Get the scrollbar width
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
console.warn(scrollbarWidth); // Mac: 15
// Delete the DIV
document.body.removeChild(scrollDiv);With the element in the page, subtracting the clientWidth from the offsetWidth gives the scrollbar size! The last step is removing the DIV from the DOM and done!
Since the scrollbar size is different between Mac and Windows (and even Internet Explorer 7 vs. other IE versions), this small but dynamic snippet is just what I needed to find the true content area of the container. Feel free to convert this JavaScript snippet into whatever JavaScript framework your prefer!
Вариант
var getScrollbarWidth = function() {
var div, width = getScrollbarWidth.width;
if (width === undefined) {
div = document.createElement('div');
div.innerHTML = '<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>';
div = div.firstChild;
document.body.appendChild(div);
width = getScrollbarWidth.width = div.offsetWidth - div.clientWidth;
document.body.removeChild(div);
}
return width;
};Вызов:
document.body.style.paddingRight = getScrollbarWidth() + 'px';Или так (вызов такой же):
function getScrollBarWidth () {
var inner = document.createElement('p');
inner.style.width = "100%";
inner.style.height = "200px";
var outer = document.createElement('div');
outer.style.position = "absolute";
outer.style.top = "0px";
outer.style.left = "0px";
outer.style.visibility = "hidden";
outer.style.width = "200px";
outer.style.height = "150px";
outer.style.overflow = "hidden";
outer.appendChild (inner);
document.body.appendChild (outer);
var w1 = inner.offsetWidth;
outer.style.overflow = 'scroll';
var w2 = inner.offsetWidth;
if (w1 == w2) w2 = outer.clientWidth;
document.body.removeChild (outer);
return (w1 - w2);
}; или:
function getScrollbarWidth() {
// Creating invisible container
const outer = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.overflow = 'scroll'; // forcing scrollbar to appear
outer.style.msOverflowStyle = 'scrollbar'; // needed for WinJS apps
document.body.appendChild(outer);
// Creating inner element and placing it in the container
const inner = document.createElement('div');
outer.appendChild(inner);
// Calculating difference between container's full width and the child width
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
// Removing temporary elements from the DOM
outer.parentNode.removeChild(outer);
return scrollbarWidth;
}Вызов:
document.body.style.paddingRight = getScrollbarWidth() + 'px';или:
function scrollbarWidth() {
// Scrollbalken im Body ausschalten
document.body.style.overflow = 'hidden';
var width = document.body.clientWidth;
// Scrollbalken
document.body.style.overflow = 'scroll';
width -= document.body.clientWidth;
// Der IE im Standardmode
if(!width) width = document.body.offsetWidth-document.body.clientWidth;
// ursprüngliche Einstellungen wiederherstellen
document.body.style.overflow = '';
return width;
}Вызов:
document.body.style.paddingRight = scrollbarWidth() + 'px';Ещё:
function getScrollbarWidth() {
var outer = document.createElement("div");
outer.style.visibility = "hidden";
outer.style.width = "100px";
outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps
document.body.appendChild(outer);
var widthNoScroll = outer.offsetWidth;
// force scrollbars
outer.style.overflow = "scroll";
// add innerdiv
var inner = document.createElement("div");
inner.style.width = "100%";
outer.appendChild(inner);
var widthWithScroll = inner.offsetWidth;
// remove divs
outer.parentNode.removeChild(outer);
return widthNoScroll - widthWithScroll;
}Вызов:
document.body.style.paddingRight = getScrollbarWidth() + 'px';