Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery detect phone orientation

Tags:

jquery

I have some code I wrote that alerts the user on mobile phones that change to landscape to basically go to portrait. This works great but if the user starts off in landscape it obviously doesnt show since there is no orientation change. Is there a way to just get the orientation of the get go?

$(window).on("orientationchange",function(){
        if( (window.orientation == 90 && $( window ).width() < 780) || (window.orientation == -90 && $( window ).width() < 780) ) // Landscape
        {
          $('body').append('<div class="phoneLandscape"><h4>Sorry, this site is best viewed in Portrait Mode</h4></div>');
        }
        else // Portrait
        {
          $('.phoneLandscape').remove();
        }
      });
like image 903
Packy Avatar asked Jun 11 '15 15:06

Packy


1 Answers

jQuery mobile has the method $(window).on("orientationchange") but if you're looking to detect orientation at any given time try this

if(window.innerHeight > window.innerWidth){
    //portrait
}
if(window.innerWidth > window.innerHeight){
    //landscape
}
like image 182
stackoverfloweth Avatar answered Oct 13 '22 01:10

stackoverfloweth