Skip to content

Instantly share code, notes, and snippets.

@Demwunz
Last active October 5, 2015 06:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Demwunz/2762725 to your computer and use it in GitHub Desktop.
Save Demwunz/2762725 to your computer and use it in GitHub Desktop.
Check if Media Query has fired, device orientation and device type (via categorizr classes)
DASEFX ={
common:{
init: function(){
//using http://benalman.com/code/projects/jquery-throttle-debounce/docs/files/jquery-ba-throttle-debounce-js.html
$(window).resize( $.throttle( 250, true, function(e){
log('resized');
DASEFX.common.MediaQueryCheck();
}));
},
MediaQueryCheck : function(){
// see http://bricss.net/post/22198838298/easily-checking-in-javascript-if-a-css-media-query-has
var size = window.getComputedStyle(document.body,':after').getPropertyValue('content');
// see https://github.com/Skookum/categorizr.js
if (size == 'mobile' && isMobile()) {
log('mobile MQ fired');
DASEFX.common.OrientationCheck();
} else if(size == 'tablet' && isTablet()){
log('tablet MQ fired');
DASEFX.common.OrientationCheck();
} else{
//must be bigger then 768 - desktop experience please.
log('desktop MQ fired');
DASEFX.common.OrientationCheck();
}
},
OrientationCheck : function(){
// detect device orientation
var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
window.addEventListener(orientationEvent, function() {
var orientation = Math.abs(window.orientation) === 90 ? 'landscape' : 'portrait';
log(orientation);
}, false);
}
}
}
/* see http://bricss.net/post/22198838298/easily-checking-in-javascript-if-a-css-media-query-has */
body:after{
content:'desktop';display:none;
}
@media only screen (max-width: 767px) {
body:after{
content:'mobile';display:none;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment