jquery检测手机方向

Pac*_*cky 5 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();
        }
      });
Run Code Online (Sandbox Code Playgroud)

sta*_*eth 8

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

if(window.innerHeight > window.innerWidth){
    //portrait
}
if(window.innerWidth > window.innerHeight){
    //landscape
}
Run Code Online (Sandbox Code Playgroud)

  • 但这不会也会影响非移动设备吗?如果您只想将其应用于移动设备怎么办? (2认同)