Skip to content

Instantly share code, notes, and snippets.

@ispmdev
Forked from scottjehl/getViewportSize.js
Created December 12, 2019 20:15
Show Gist options
  • Select an option

  • Save ispmdev/d002f29c6395d3212b5ccc68d40af5bf to your computer and use it in GitHub Desktop.

Select an option

Save ispmdev/d002f29c6395d3212b5ccc68d40af5bf to your computer and use it in GitHub Desktop.

Revisions

  1. Scott Jehl created this gist Mar 16, 2012.
    23 changes: 23 additions & 0 deletions getViewportSize.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    /*!
    An experiment in getting accurate visible viewport dimensions across devices
    (c) 2012 Scott Jehl.
    MIT/GPLv2 Licence
    */

    function viewportSize(){
    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;
    }


    // Notes:
    // relies on position:fixed support, but it should work in browsers that partially support position: fixed like iOS4 and such...

    //sample usage: var viewportwidth = viewportSize().width;