我们如何找到滚动条的宽度?

时间:2009-12-10 09:23:08

标签: javascript

您知道任何跨浏览器的方法吗?

3 个答案:

答案 0 :(得分:2)

没有任何方式跨浏览器工作(IE就像往常一样),抱歉

答案 1 :(得分:1)

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;  
}

答案 2 :(得分:0)

以下是MooTools中的一个示例,我刚刚完成了完整的跨浏览器测试。

http://jsfiddle.net/jP6q2/2/

长话短说:

var body = $$('body')[0];

var test = new Element('div', {'style':'visibility:hidden; width: 100px; height: 100px; overflow: scroll;'});

test.inject(body);

var scrollbar = {
    width: test.offsetWidth - test.scrollWidth
    , height: test.offsetHeight - test.scrollHeight
}

test.dispose();