window.innerHeight ie8替代

The*_*lis 26 javascript jquery internet-explorer-8

我有一些依赖的计算window.innerHeight.但正如我发现这在IE9之前的任何IE中都不可用.我所看到的所有其他选项甚至都没有接近我使用时得到的数字window.innerHeight.

有人有工作吗?

San*_*hak 48

您可能想尝试:

document.documentElement.clientHeight;
Run Code Online (Sandbox Code Playgroud)

或者可以使用jQuery的.height()方法:

$(window).height();
Run Code Online (Sandbox Code Playgroud)

  • +1`document.body.clientHeight`在所有情况下都不适合我,但我发现`$(window).height()`是可靠的 (5认同)
  • todo:阅读`document.documentElement.clientHeight` (4认同)
  • Dan是对的,真正等同于`$(window).height()`是`document.documentElement.clientHeight`,而不是`document.body.clientHeight`.这里是测试小提琴:http://jsfiddle.net/tzdcx/1/或http://jsfiddle.net/tzdcx/1/show(适用于IE7及以上版本) (4认同)

yck*_*art 14

我为IE8和其他人制作了一个简单的垫片:

  • window.innerWidth
  • window.innerHeight
  • window.scrollX
  • window.scrollY
  • document.width
  • document.height

559个字节

(function(d,b){var c=b.documentElement;var a=b.body;var e=function(g,h,f){if(typeof g[h]==="undefined"){Object.defineProperty(g,h,{get:f})}};e(d,"innerWidth",function(){return c.clientWidth});e(d,"innerHeight",function(){return c.clientHeight});e(d,"scrollX",function(){return d.pageXOffset||c.scrollLeft});e(d,"scrollY",function(){return d.pageYOffset||c.scrollTop});e(b,"width",function(){return Math.max(a.scrollWidth,c.scrollWidth,a.offsetWidth,c.offsetWidth,a.clientWidth,c.clientWidth)});e(b,"height",function(){return Math.max(a.scrollHeight,c.scrollHeight,a.offsetHeight,c.offsetHeight,a.clientHeight,c.clientHeight)});return e}(window,document));
Run Code Online (Sandbox Code Playgroud)

有关更新,请查看此要点:yckart/9128d824c7bdbab2832e

(function (window, document) {

  var html = document.documentElement;
  var body = document.body;

  var define = function (object, property, getter) {
    if (typeof object[property] === 'undefined') {
      Object.defineProperty(object, property, { get: getter });
    }
  };

  define(window, 'innerWidth', function () { return html.clientWidth });
  define(window, 'innerHeight', function () { return html.clientHeight });

  define(window, 'scrollX', function () { return window.pageXOffset || html.scrollLeft });
  define(window, 'scrollY', function () { return window.pageYOffset || html.scrollTop });

  define(document, 'width', function () { return Math.max(body.scrollWidth, html.scrollWidth, body.offsetWidth, html.offsetWidth, body.clientWidth, html.clientWidth) });
  define(document, 'height', function () { return Math.max(body.scrollHeight, html.scrollHeight, body.offsetHeight, html.offsetHeight, body.clientHeight, html.clientHeight) });

  return define;

}(window, document));
Run Code Online (Sandbox Code Playgroud)