简体   繁体   English

如何在jQuery中正确检测移动设备的方向?

[英]How to detect orientation of mobile device properly in jQuery?

I am writing some jQuery code that i want to detect the mobile devices orientation whether portrait or landscape. 我正在编写一些jQuery代码,我想检测移动设备的方向(纵向还是横向)。

I have already tried while statements but they send my test alert constantly with no avail. 我已经尝试了while语句,但是它们不断发送我的测试警报,但无济于事。

my jQuery: 我的jQuery:

function test() {
    if (window.matchMedia("(orientation: portrait)").matches) {
     alert('You are in portrait!');
  }

  else if (window.matchMedia("(orientation: landscape)").matches) {
     alert('You are in landscape!');
  }
}
  test();

Currently this runs as soon as you load into the page so that I could test the results. 目前,此操作会在您加载到页面后立即运行,以便我可以测试结果。 What I would like it to do is detect if the device is using landscape and change some CSS if it is. 我要执行的操作是检测设备是否正在使用风景,如果有,请更改一些CSS。 The problem is is that it works properly at detecting it just when I turn the device into landscape it does not detect properly. 问题在于,仅当我将设备转为横向时,它才能正确检测到它,但无法正确检测到它。 It detects portrait or landscape if I refresh the page while i have the device in that orientation. 如果将设备放在该方向时刷新页面,它将检测到纵向还是横向。 What I want to happen is for it to detect all the time as soon as I put the device into landscape mode I want it to change the CSS to the proper CSS for landscape and when it goes back to portrait I want it to change back to the original CSS. 我想发生的事情是,只要将设备置于横向模式,它就可以一直检测到所有时间,我希望它可以将CSS更改为适合横向的CSS,并且当它返回纵向时,我希望将其更改为原始的CSS。 But in this case for testing and for you guys I want it to alert properly every time I change the device orientation not when i refresh the page in that orientation. 但是在这种情况下,为了进行测试并为你们服务,我希望它在每次更改设备方向时都能正确地发出警报,而不是当我以该方向刷新页面时。

I actually just figured out a way using the switch method. 我实际上只是想出一种使用switch方法的方法。 For some reason case -90 || 出于某种原因-90 || 90: was not working properly so i just made one for each proper orientation like so. 90:工作不正常,所以我只是像这样针对每个正确的方向做一个。

function doOnOrientationChange() {
    switch(window.orientation) {
      case 90:
        alert('landscape');
        break;
      case -90:
        alert('landscape');
        break;
      case 0:
        alert('portrait');
        break;
      case 180:
        alert('portrait');
        break;
      default:
        break;
    }
}

window.addEventListener('orientationchange', doOnOrientationChange);

// Initial execution if needed
doOnOrientationChange();

Thank you everyone for your help! 谢谢你们每一个人的帮助! This works for me on all devices I have to test on! 这对我所有需要测试的设备都适用!

Use orientationchange . 使用orientationchange It is fired when the orientation of the device has changed. 设备的方向更改时将触发该事件。 You can find more about this from here: https://developer.mozilla.org/en-US/docs/Web/API/Window/orientationchange_event 您可以在这里找到有关此的更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Window/orientationchange_event

 function detectMobileOrientation() { switch(screen.orientation.angle) { case 90: case 270: alert('landscape' + screen.orientation.angle); break; default: alert('portrait' + screen.orientation.angle); break; } } window.addEventListener("orientationchange", detectMobileOrientation); detectMobileOrientation() 

You can test this directly using this link . 您可以使用此链接直接测试。 I have tested the code on Android 9 and it is working fine for me. 我已经在Android 9上测试了代码,并且对我来说工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM