如何获得浏览器的滚动条大小?

glm*_*ndr 157 javascript scrollbar

如何在JavaScript中确定水平滚动条的高度或垂直滚动​​条的宽度?

Mat*_*nes 133

来自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);
};
Run Code Online (Sandbox Code Playgroud)

  • 使用不同的页面缩放返回不同的值.Win7,Opera,FF. (6认同)
  • 这个想法是天才,我肯定会在此基础上制作一个MooTools课程. (2认同)

Jos*_*ick 79

使用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;
};
Run Code Online (Sandbox Code Playgroud)

  • 如果JQuery的整个源代码在它之前被复制粘贴,这个解决方案真的会感觉更干净吗?因为这就是这个解决方案正在做的事情.这个问题并未在JQuery中寻求答案,并且在不使用库的情况下执行此任务是完全可能且高效的. (4认同)
  • 如果你已经在使用JQuery,那么守护进程的评论是无关紧要的.是的,添加JQuery只是为了做到这一点是无稽之谈,但对于那些已经在他们的项目中使用JQuery的人来说,这是一个比接受的更简单的解决方案. (4认同)
  • 谢谢,这个解决方案非常干净! (2认同)
  • 比投票的解决方案更清洁 (2认同)

Jan*_*nek 25

这只是我发现的脚本,它在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;
};
Run Code Online (Sandbox Code Playgroud)

最小化版本:

$.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};
Run Code Online (Sandbox Code Playgroud)

当文档准备就绪时你必须调用它......所以

$(function(){ console.log($.scrollbarWidth()); });
Run Code Online (Sandbox Code Playgroud)

在Windows 7上测试了最新的FF,Chrome,IE和Safari以及100%正常运行的2012-03-28.

来源:http://benalman.com/projects/jquery-misc-plugins/#scrollbarwidth

  • @MartinAnsty但[width]变量在函数内部声明*,因此每次调用函数时都会重新创建它. (17认同)
  • 宽度将始终===在该代码中未定义.也许如果(真){...} (13认同)
  • @MartinAnsty,如果你看一下[source](https://github.com/cowboy/jquery-misc/blob/master/jquery.ba-scrollbarwidth.js#L12),它会在外部闭包中声明. (4认同)
  • 更正:`width`将_always_ === undefined**函数被调用的第一个**时间.在后续对函数"width"的调用已经设置好后,该检查只会阻止计算再次不必要地运行. (3认同)
  • 为了强调@sstur和@TheCloudlessSky的观点,上面的代码与Ben Alman的插件中的代码相同,并且它不会将结果缓存在`width`中,而是每次重新计算它.它有效,但效率非常低.请帮助世界,并在Alman的插件中使用[正确版本](https://github.com/cowboy/jquery-misc/blob/master/jquery.ba-scrollbarwidth.js). (2认同)

Bee*_*exe 23

如果你正在寻找一个简单的操作,只需混合普通的dom js和jquery,

var swidth=(window.innerWidth-$(window).width());
Run Code Online (Sandbox Code Playgroud)

返回当前页面滚动条的大小.(如果它是可见的,否则将返回0)

  • 如果窗口没有滚动条(大显示器或小页面/应用程序),这将失败 (10认同)

Jos*_*ola 15

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; 
} 
Run Code Online (Sandbox Code Playgroud)

  • 如果身体有一个固定的宽度,则不起作用. (6认同)

Mem*_*sen 9

我找到了一个简单的解决方案,适用于页面内部的元素,而不是页面本身: $('#element')[0].offsetHeight - $('#element')[0].clientHeight

这将返回x轴滚动条的高度.


And*_*eno 6

对我来说,最有用的方式是

(window.innerWidth - document.getElementsByTagName('html')[0].clientWidth)
Run Code Online (Sandbox Code Playgroud)

用香草JavaScript.

在此输入图像描述

  • 正是我所需要的。一个建议:第二项可以用`document.documentElement.clientWidth`代替。`documentElement`更清晰,干净地表达了获取`&lt;html&gt;`元素的意图。 (3认同)

Ale*_*der 6

这应该可以解决问题,不是吗?

function getScrollbarWidth() {
  return (window.innerWidth - document.documentElement.clientWidth);
}
Run Code Online (Sandbox Code Playgroud)


dev*_*wan 5

您可以使用 jquery + javascript 确定window滚动条,document如下所示:

var scrollbarWidth = ($(document).width() - window.innerWidth);
console.info("Window Scroll Bar Width=" + scrollbarWidth );
Run Code Online (Sandbox Code Playgroud)


mpe*_*pen 5

来自David Walsh的博客:

// 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);
Run Code Online (Sandbox Code Playgroud)
.scrollbar-measure {
	width: 100px;
	height: 100px;
	overflow: scroll;
	position: absolute;
	top: -9999px;
}
Run Code Online (Sandbox Code Playgroud)

在我的网站上给了我17,在Stackoverflow上给了我14.