【问题标题】:What's the raw JavaScript equivalent to jQuery's $(window).width什么是原始 JavaScript 等价于 jQuery 的 $(window).width
【发布时间】:2012-01-03 12:06:25
【问题描述】:

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

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

【问题讨论】:

    标签: jquery width


    【解决方案1】:

    这个

    window.innerWidth
    

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

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

    但是,IE8 没有实现这个属性...

    【讨论】:

      【解决方案2】:
      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

      【讨论】:

        【解决方案3】:

        See 为你自己。 它使用不同的东西。 document.documentElement.clientWidth 就是其中之一。

        也可以使用document.body.clientWidth

        【讨论】:

          猜你喜欢
          • 2011-10-14
          • 1970-01-01
          • 2017-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-29
          • 2011-03-19
          • 1970-01-01
          相关资源
          最近更新 更多