Javascript window.innerHeight ie8 替代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10173236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:09:50  来源:igfitidea点击:

window.innerHeight ie8 alternative

javascriptjqueryinternet-explorer-8

提问by Theo Kouzelis

I have some calculations that rely on window.innerHeight. But as I have found out this is not available in any IE before IE9. All the other options I have looked at don't even come close to the figure I get when I use window.innerHeight.

我有一些依赖于window.innerHeight. 但正如我发现这在 IE9 之前的任何 IE 中都不可用。我看过的所有其他选项甚至都与我使用window.innerHeight.

Does anybody have a work around?

有人有解决办法吗?

回答by Sandeep Pathak

You might want to try:

您可能想尝试:

document.documentElement.clientHeight;

Or can use jQuery's .height()method:

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

$(window).height();

回答by yckart

I made a simple shim for IE8 and others:

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

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

559 bytes

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));

For updates, take a look at this gist: yckart/9128d824c7bdbab2832e

有关更新,请查看此要点: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));

回答by Akshay

Use following in your document.readyand define innerHeightexplicitly.

在您的document.readyinnerHeight明确定义中使用以下内容。

window.innerHeight = window.innerHeight || document.documentElement.clientHeight

回答by Max Leps

Javascript method to get full window height..:

获取完整窗口高度的Javascript方法..:

window.outerHeight;

Javascript methods to get full document height..:

获取完整文档高度的 Javascript 方法..:

document.body.clientHeight;

or

或者

document.body.offsetHeight;

Javascript methods to get visible document area height..:

获取可见文档区域高度的 Javascript 方法..:

this is height of window without bars.

这是没有栏的窗口的高度。

window.innerHeight;

jQuery methods to get visible document area height and window without bars area height too..:

jQuery 方法来获取可见文档区域高度和没有条形区域高度的窗口..:

$(window).height();

or

或者

$(window).outerHeight();

jQuery methods to get full document height..:

获取完整文档高度的 jQuery 方法..:

$(document).height();

or

或者

$(document).outerHeight();

that's all, I hope will help you :)

就是这样,希望能帮到你:)

回答by scorp

I searched on because n o n e of the here posted functions seemed to work, i always got the windowviewheight not the documents full height...

我搜索是因为这里发布的功能似乎都不起作用,我总是得到 windowviewheight 而不是文档全高...

this works in all browsers I've tested...also iexplore 8:

这适用于我测试过的所有浏览器......还有 iexplore 8:

var D = document;
var myHeight = Math.max(
    D.body.scrollHeight, D.documentElement.scrollHeight,
    D.body.offsetHeight, D.documentElement.offsetHeight,
    D.body.clientHeight, D.documentElement.clientHeight
);

alert(myHeight);