Last active
August 29, 2015 13:55
-
-
Save IOZ/8787497 to your computer and use it in GitHub Desktop.
Get window viewport width and height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get window viewport width | |
* @param side {string} - (width|height|both) | |
* | |
* Usage getViewPort('width'); // return viewport width | |
*/ | |
function getViewPort(side){ | |
var side = side || 'both'; | |
var viewport = { width: 0, height: 0 }; | |
if (typeof window.innerWidth != 'undefined') { | |
viewport.width = window.innerWidth; | |
viewport.height = window.innerHeight; | |
} | |
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document) | |
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) { | |
viewport.width = document.documentElement.clientWidth; | |
viewport.height = document.documentElement.clientHeight; | |
} | |
// older versions of IE | |
else { | |
viewport.width = document.getElementsByTagName('body')[0].clientWidth; | |
viewport.height = document.getElementsByTagName('body')[0].clientHeight; | |
} | |
switch(side){ | |
case 'width': | |
return viewport.width; | |
break; | |
case 'height': | |
return viewport.height; | |
break; | |
case 'both': | |
return viewport; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment