Created
October 25, 2012 18:07
-
-
Save Ilink/3954381 to your computer and use it in GitHub Desktop.
Device Rotation Event
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
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