相当于 jQuery 的 $(window).width 的原始 JavaScript 是什么?

发布于 2024-12-17 11:19:43 字数 277 浏览 1 评论 0原文

我听说它是​​ offset.width 、 document.documentElement.clientWidth 和 window.innerWidth

我对无法使用 jQuery 的项目很好奇,我应该使用哪种解决方案?

I've heard it's offset.width, document.documentElement.clientWidth, and window.innerWidth

I'm curious for projects which I cannot use jQuery on, which solution I should use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走野 2024-12-24 11:19:43
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

function windowWidth() {
    var docElemProp = window.document.documentElement.clientWidth,
        body = window.document.body;
    return window.document.compatMode === "CSS1Compat" && docElemProp || body && body.clientWidth || docElemProp;
}

Taken (and modified slightly from the jQuery source:

https://github.com/jquery/jquery/blob/master/src/dimensions.js#L42

夜深人未静 2024-12-24 11:19:43

亲自查看
它使用不同的东西。 document.documentElement.clientWidth 就是其中之一。

也可以使用 document.body.clientWidth

See for yourself.
It uses different things. document.documentElement.clientWidth is among them.

It can also use document.body.clientWidth

感情废物 2024-12-24 11:19:43

上述

window.innerWidth

属性返回与 $( window ).width() 相同的值。

现场演示: http://jsfiddle.net/Jukh9/1/show/

然而,IE8 并没有实现这个属性...

This

window.innerWidth

The above property returns the same value as $( window ).width().

Live demo: http://jsfiddle.net/Jukh9/1/show/

However, IE8 does not implement this property...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文