JavaScriptで水平スクロールバーの高さ、または垂直スクロールバーの幅を決定するにはどうすればよいですか?
25 に答える
jQuery を使用すると、Matthew Vines の回答を次のように短縮できます。
function getScrollBarWidth () {
var $outer = $('<div>').css({visibility: 'hidden', width: 100, overflow: 'scroll'}).appendTo('body'),
widthWithScroll = $('<div>').css({width: '100%'}).appendTo($outer).outerWidth();
$outer.remove();
return 100 - widthWithScroll;
};
単純な操作を探している場合は、単純な dom js と jquery を混ぜてください。
var swidth=(window.innerWidth-$(window).width());
現在のページのスクロールバーのサイズを返します。(表示されている場合、そうでない場合は 0 を返します)
これは私が見つけた唯一のスクリプトで、Webkit ブラウザーで動作しています ... :)
$.scrollbarWidth = function() {
var parent, child, width;
if(width===undefined) {
parent = $('<div style="width:50px;height:50px;overflow:auto"><div/></div>').appendTo('body');
child=parent.children();
width=child.innerWidth()-child.height(99).innerWidth();
parent.remove();
}
return width;
};
最小化されたバージョン:
$.scrollbarWidth=function(){var a,b,c;if(c===undefined){a=$('<div style="width:50px;height:50px;overflow:auto"><div/></div>').appendTo('body');b=a.children();c=b.innerWidth()-b.height(99).innerWidth();a.remove()}return c};
そして、ドキュメントの準備ができたら呼び出す必要があります...だから
$(function(){ console.log($.scrollbarWidth()); });
2012 年 3 月 28 日に Windows 7 で最新の FF、Chrome、IE、Safari をテストし、100% 動作しました。
ソース: http://benalman.com/projects/jquery-misc-plugins/#scrollbarwidth
window.scrollBarWidth = function() {
document.body.style.overflow = 'hidden';
var width = document.body.clientWidth;
document.body.style.overflow = 'scroll';
width -= document.body.clientWidth;
if(!width) width = document.body.offsetWidth - document.body.clientWidth;
document.body.style.overflow = '';
return width;
}
ページ自体ではなく、ページ内の要素に対して機能する簡単な解決策を見つけました。
$('#element')[0].offsetHeight - $('#element')[0].clientHeight
これは、x 軸スクロールバーの高さを返します。
// 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.info(scrollbarWidth); // Mac: 15
// Delete the DIV
document.body.removeChild(scrollDiv);
.scrollbar-measure {
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
}
私のウェブサイトでは 17、Stackoverflow では 14 です。
jquery + javascriptを使用して、以下のようにwindow
スクロールバーを決定できます。document
var scrollbarWidth = ($(document).width() - window.innerWidth);
console.info("Window Scroll Bar Width=" + scrollbarWidth );
スクロールバーのある要素が既にある場合は、次を使用します。
function getScrollbarHeight(el) {
return el.getBoundingClientRect().height - el.scrollHeight;
};
horzintscrollbar が存在しない場合、関数は 0 を返します。
Antiscroll.js
そのコードでそれを行う方法は次のとおりです。
function scrollbarSize () {
var div = $(
'<div class="antiscroll-inner" style="width:50px;height:50px;overflow-y:scroll;'
+ 'position:absolute;top:-200px;left:-200px;"><div style="height:100px;width:100%"/>'
+ '</div>'
);
$('body').append(div);
var w1 = $(div).innerWidth();
var w2 = $('div', div).innerWidth();
$(div).remove();
return w1 - w2;
};
コードはこちらから: https://github.com/LearnBoost/antiscroll/blob/master/antiscroll.js#L447
detectScrollbarWidthHeight: function() {
var div = document.createElement("div");
div.style.overflow = "scroll";
div.style.visibility = "hidden";
div.style.position = 'absolute';
div.style.width = '100px';
div.style.height = '100px';
document.body.appendChild(div);
return {
width: div.offsetWidth - div.clientWidth,
height: div.offsetHeight - div.clientHeight
};
},
Chrome、FF、IE8、IE11 でテスト済み。
jquery の場合 (firefox でのみテスト済み):
function getScrollBarHeight() {
var jTest = $('<div style="display:none;width:50px;overflow: scroll"><div style="width:100px;"><br /><br /></div></div>');
$('body').append(jTest);
var h = jTest.innerHeight();
jTest.css({
overflow: 'auto',
width: '200px'
});
var h2 = jTest.innerHeight();
return h - h2;
}
function getScrollBarWidth() {
var jTest = $('<div style="display:none;height:50px;overflow: scroll"><div style="height:100px;"></div></div>');
$('body').append(jTest);
var w = jTest.innerWidth();
jTest.css({
overflow: 'auto',
height: '200px'
});
var w2 = jTest.innerWidth();
return w - w2;
}
しかし、実際には@Steveの答えの方が好きです。
動作しているように見えますが、すべてのブラウザで動作するより簡単なソリューションがあるのではないでしょうか?
// 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.info(scrollbarWidth); // Mac: 15
// Delete the DIV
document.body.removeChild(scrollDiv);
.scrollbar-measure {
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
}