我如何在JavaScript中确定水平滚动条的高度或垂直滚动条的宽度?
我如何在JavaScript中确定水平滚动条的高度或垂直滚动条的宽度?
来源于Alexandre Gomes博客,我没有尝试过。如果它对你有用,请告诉我。
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 () {
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;
};
document.documentElement.clientWidth
。 documentElement
更清晰、更干净地表达了获取 <html>
元素的意图。 - Jan Miksovsky这是我找到的唯一一个在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日在最新版本的FF、Chrome、IE和Safari上测试通过,适用于Windows 7操作系统,百分之百可行。
来源:http://benalman.com/projects/jquery-misc-plugins/#scrollbarwidth
width
将始终等于未定义(undefined)。在对函数进行后续调用时,width
已经被设置,该检查只是为了避免不必要地再次运行计算。 - MartinAnsty如果你要进行简单的操作,只需混合使用普通的Dom JS和jQuery。
var swidth=(window.innerWidth-$(window).width());
返回当前页面滚动条的大小。(如果可见,则返回大小;否则返回0)
$(element).width()
是什么意思?如何在没有jQuery的情况下编写它? - Micah Zoltuwindow.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分。
window
滚动条可以通过以下代码与 document
相关联:
var scrollbarWidth = ($(document).width() - window.innerWidth);
console.info("Window Scroll Bar Width=" + scrollbarWidth );
function getScrollbarWidth() {
return (window.innerWidth - document.documentElement.clientWidth);
}