【发布时间】:2012-01-03 12:06:25
【问题描述】:
我听说是offset.width、document.documentElement.clientWidth 和 window.innerWidth
我对无法使用 jQuery 的项目很好奇,我应该使用哪种解决方案?
【问题讨论】:
我听说是offset.width、document.documentElement.clientWidth 和 window.innerWidth
我对无法使用 jQuery 的项目很好奇,我应该使用哪种解决方案?
【问题讨论】:
这个
window.innerWidth
上述属性返回与$( window ).width()相同的值。
现场演示: http://jsfiddle.net/Jukh9/1/show/
但是,IE8 没有实现这个属性...
【讨论】:
function windowWidth() {
var docElemProp = window.document.documentElement.clientWidth,
body = window.document.body;
return window.document.compatMode === "CSS1Compat" && docElemProp || body && body.clientWidth || docElemProp;
}
取自(并根据 jQuery 源代码稍作修改:
https://github.com/jquery/jquery/blob/master/src/dimensions.js#L42
【讨论】:
See 为你自己。 它使用不同的东西。 document.documentElement.clientWidth 就是其中之一。
也可以使用document.body.clientWidth
【讨论】: