Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I determine if a device is in portrait or landscape mode using jquery?

I want to conditionally alter what the user sees on the photo gallery web site I'm going to create based on if the device being used to view the site is in portrait/vertical vs. landscape/horizontal mode/orientation. Is this possible?

like image 722
B. Clay Shannon-B. Crow Raven Avatar asked Apr 12 '13 20:04

B. Clay Shannon-B. Crow Raven


2 Answers

Try the orientationchange event handler, something like this:

$(window).bind("orientationchange", function(evt){
    alert(evt.orientation);
});

Here's the jQuery Mobile entry on detecting and firing the orientationchange event.

like image 63
Abbas Avatar answered Sep 21 '22 15:09

Abbas


If you are just changing the layout, consider CSS Media Queries instead.

In Chrome 7 and Firefox 3.6, there is the deviceorientation event which you can listen to.


In response to your comment, a simple way of detecting tablets and phones is to detect the screen resolution and size. Tablets generally have higher display resolutions than phones. Media Queries can determine these factors.

As for tablet-phone options

  • If your device is a phone, you can toggle CSS using Media Queries based on device orientation.

  • If the device is a tablet, you can toggle the CSS using JS using the body id/class switching or by swapping out stylesheets.

like image 40
Joseph Avatar answered Sep 19 '22 15:09

Joseph