如何使用javascript和Phonegap在iOS中正确检测方向更改? [英] How do I correctly detect orientation change using javascript and Phonegap in IOS?

查看:109
本文介绍了如何使用javascript和Phonegap在iOS中正确检测方向更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个方向测试代码下面寻找JQTouch参考资料。这在iOS模拟器在移动safari正确工作,但不能在Phonegap中正确处理。我的项目正在运行到杀死此测试页的同一个问题。有没有办法使用javascript在Phonegap中感知方向变化?

I found this orientation test code below looking for JQTouch reference material. This works correctly in the IOS simulator in mobile safari but doesn't handle correctly in Phonegap. My project is running into the same issue that is killing this test page. Is there a way to sense the orientation change using javascript in Phonegap?

任何帮助都会感激。

window.onorientationchange = function() {
      /*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
        left, or landscape mode with the screen turned to the right. */
      var orientation = window.orientation;
      switch(orientation) {
        case 0:
            /* If in portrait mode, sets the body's class attribute to portrait. Consequently, all style definitions matching the body[class="portrait"] declaration
               in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
            document.body.setAttribute("class","portrait");

            /* Add a descriptive message on "Handling iPhone or iPod touch Orientation Events"  */
            document.getElementById("currentOrientation").innerHTML="Now in portrait orientation (Home button on the bottom).";
            break; 

        case 90:
            /* If in landscape mode with the screen turned to the left, sets the body's class attribute to landscapeLeft. In this case, all style definitions matching the
               body[class="landscapeLeft"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
            document.body.setAttribute("class","landscape");

            document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the left (Home button to the right).";
            break;

        case -90: 
            /* If in landscape mode with the screen turned to the right, sets the body's class attribute to landscapeRight. Here, all style definitions matching the
               body[class="landscapeRight"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
            document.body.setAttribute("class","landscape");

            document.getElementById("currentOrientation").innerHTML="Now in landscape orientation and turned to the right (Home button to the left).";
            break;
      }
    }


推荐答案

我所做的:

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

  window.addEventListener('orientationchange', doOnOrientationChange);

  // Initial execution if needed
  doOnOrientationChange();
</script>

这篇关于如何使用javascript和Phonegap在iOS中正确检测方向更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆