Created
January 13, 2016 13:03
-
-
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.
This file contains hidden or 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
'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