Skip to content

Instantly share code, notes, and snippets.

@Ilink
Created October 25, 2012 18:07
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 Ilink/3954381 to your computer and use it in GitHub Desktop.
Save Ilink/3954381 to your computer and use it in GitHub Desktop.
Device Rotation Event
function setup_orientation(){
function is_landscape(){
if($(window).width() >= $(window).height()){
return true;
} else {
return false;
}
}
var landscape = false;
// These are for initial orientation
if(is_landscape()){
landscape = true;
$(document).trigger('orientation_change', {orientation: 'landscape'});
} else {
$(document).trigger('orientation_change', {orientation: 'portrait'});
}
$(window).resize(function() {
if(is_landscape()){
if(!landscape) {
$(document).trigger('orientation_change', {orientation: 'landscape'});
landscape = true;
}
} else {
if(landscape) {
landscape = false;
$(document).trigger('orientation_change', {orientation: 'portrait'});
}
}
});
};
// Let's use the event here
$(document).ready(function(){
setup_orientation();
$(document).on('orientation_change', function(e, data){
if(data.orientation === 'portrait'){
console.log('portrait');
} else {
console.log('landscape');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment