开发者

How do we find the width of scroll bar?

Do you know any cross-brow开发者_如何学Goser methods?


There aren't any way working cross browser (IE is the problem, as always), sorry


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


Here is an example in MooTools, and I just finished full cross-browser testing.

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

Long story short:

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜