开发者

set body width in px?

how can you set the body width in px in javascript?

has to work in chrome, ff and our beloved ie

edit:

it has to be used when a dialog box pops up and the horisontal scrollbar is hidden.. then I have to compensate for the missing 16px.. els开发者_Go百科e the whole site is moving slightly to the right

maybe you have better solution to the problem? I don't know :)


document.body.style.width = '800px';

[Edit]

If you need to adjust from the existing width, per your edit, you could do something like this:

var adjustBodyWidth = function(nPixels) {
  var m, w = document.body.style.width || document.body.clientWidth;
  if (m=w.match(/^(\d+)px$/)) {
    document.body.style.width = (Number(m[1]) + nPixels) + 'px';
  }
};


If the problem is the scroll bar coming up and disappearing depending on the page's contents, I would consider forcing the page to display a (disabled) scroll bar even when there's nothing to scroll. This can be done through CSS:

body { overflow-y: scroll }

it works in all major browsers. It doesn't in IE, but that doesn't matter because IE shows the desired behaviour automatically.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜