Skip to content

Instantly share code, notes, and snippets.

@mrmartineau
Last active November 18, 2017 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmartineau/f850b8700f8894280129 to your computer and use it in GitHub Desktop.
Save mrmartineau/f850b8700f8894280129 to your computer and use it in GitHub Desktop.
Get viewport dimensions
/* ==========================================================================
Reliably get viewport dimensions
Notes:
relies on position:fixed support, but it should work in browsers that
partially support position: fixed like iOS4 and such...
Usage:
* $('.spotlight').css( getViewportDimensions() );
* $('.spotlight').css('height', getViewportDimensions().height);
* var viewportwidth = getViewportDimensions.width;
========================================================================== */
const getViewportDimensions = () => {
var test = document.createElement( "div" );
test.style.cssText = "position: fixed;top: 0;left: 0;bottom: 0;right: 0;";
document.documentElement.insertBefore( test, document.documentElement.firstChild );
var dims = { width: test.offsetWidth, height: test.offsetHeight };
document.documentElement.removeChild( test );
return dims;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment