Skip to content

Instantly share code, notes, and snippets.

@Wind4
Forked from yckart/README.md
Created April 13, 2016 12:03
Show Gist options
  • Save Wind4/ad4f78868f58906e67f8dfd21d417da9 to your computer and use it in GitHub Desktop.
Save Wind4/ad4f78868f58906e67f8dfd21d417da9 to your computer and use it in GitHub Desktop.
Get Viewport dimensions in all browsers. http://stackoverflow.com/a/18136089/1250044
(function (window, document) {
var html = document.getElementsByTagName('html')[0];
var body = document.getElementsByTagName('body')[0];
var ObjectDefineProperty = Object.defineProperty;
function define(object, property, getter) {
if (!(property in object)) {
ObjectDefineProperty(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; });
// OBSOLETE https://developer.mozilla.org/en-US/docs/Web/API/document.height
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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment