相当于 jQuery 的 $(window).width 的原始 JavaScript 是什么? [英] What's the raw JavaScript equivalent to jQuery's $(window).width

查看:27
本文介绍了相当于 jQuery 的 $(window).width 的原始 JavaScript 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

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

采用(并从 jQuery 源代码稍作修改:

Taken (and modified slightly from the jQuery source:

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

这篇关于相当于 jQuery 的 $(window).width 的原始 JavaScript 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆