jquery检测手机方向

时间:2015-06-11 15:51:57

标签: jquery

我写了一些代码,用于提醒手机上的用户改变景观,基本上是肖像。这很有效,但如果用户在横向开始,它显然没有显示,因为没有方向改变。有没有办法让你获得前进的方向?

$(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();
        }
      });

1 个答案:

答案 0 :(得分:6)

jQuery mobile的方法为$(window).on("orientationchange"),但如果你想在任何特定的时间检测方向,请试试这个

if(window.innerHeight > window.innerWidth){
    //portrait
}
if(window.innerWidth > window.innerHeight){
    //landscape
}