Skip to content

Instantly share code, notes, and snippets.

@peponi
Created January 13, 2016 13:03
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 peponi/48f5a602c4686f7a56b2 to your computer and use it in GitHub Desktop.
Save peponi/48f5a602c4686f7a56b2 to your computer and use it in GitHub Desktop.
A small helper, which detects orientation of viewport. Sets 'is-landscape' or 'is-portrait' to the html element.
'use strict';
(function(){
window.NIJS = window.NIJS || {};
NIJS.detection = NIJS.detection || {};
var html = document.getElementsByTagName('html')[0],
removeClass = function(str, className){
var classArr = str.split(' '),
index = -1;
for(var i = classArr.length; i--; ) {
if(classArr[i] == className){
index = i;
break;
}
}
if(index > -1) {
classArr.splice(index, 1);
}
return classArr.join(' ');
};
function detectOrientation(){
html.className = removeClass(html.className, 'is-landscape');
html.className = removeClass(html.className, 'is-portrait');
if(window.innerHeight > window.innerWidth){
// is portrait
html.className += ' is-portrait';
NIJS.detection.orientation = 'portrait';
}else {
// is landscape
html.className += ' is-landscape';
NIJS.detection.orientation = 'landscape';
}
}
window.addEventListener('resize', detectOrientation);
// set initial value
detectOrientation();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment