tôi đã thực hiện một shim đơn giản cho IE8 và những người khác:
window.innerWidth
window.innerHeight
window.scrollX
window.scrollY
document.width
document.height
559 byte
(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));
Để cập nhật, hãy nhìn vào ý chính sau: 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));
Sự cố này đã được giải quyết trong một số thư viện như [jQuery] (http://jquery.com), [Prototype] (http://prototypejs.org), [YUI] (http://developer.yahoo.com/yui/), [Đóng cửa] (http://code.google.com/closure/library) hoặc [bất kỳ số nào khác] (http://en.wikipedia.org/wiki/List_of_Java Script_libraries). Thay vì tự giải quyết nó, bạn có thể xem xét tận dụng công việc của người khác để bạn có thể tập trung vào viết mã cụ thể cho các yêu cầu của bạn. Cách khác, xem cách họ làm điều đó và sử dụng mã đó ngay cả khi bạn không sử dụng thư viện. –
Điều này đã được trả lời trước một thời gian trước đây. Kiểm tra [this] (http://stackoverflow.com/questions/5864467/internet-explorer-innerheight) – AurA
Ngoại trừ iOS '$ (cửa sổ) .height()' và 'window.innerHeight' trả về các giá trị khác nhau. Vì vậy, tôi sẽ nói rằng vấn đề vẫn chưa được giải quyết trong các thư viện và lệnh bảo đảm như vậy. – cjbarth