我听说是offset.width 、 document.documentElement.clientWidth 和 window.innerWidth
我对无法使用 jQuery 的项目感到好奇,我应该使用哪种解决方案?
最佳答案
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
关于jquery - 相当于 jQuery 的 $(window).width 的原始 JavaScript 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8221172/