如何在 iOS 上使用 Phonegap 正确检测方向变化?
我在下面寻找 JQTouch 参考资料时发现了这个方向测试代码。这在移动 Safari 上的 iOS 模拟器中可以正常工作,但在 Phonegap 中无法正确处理。我的项目遇到了与杀死此测试页面相同的问题。有没有办法在 Phonegap 中使用 JavaScript 来感知方向变化?
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;
}
}
I found this orientation test code below looking for JQTouch reference material. This works correctly in the iOS simulator on mobile Safari but doesn’t get handled 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这就是我所做的:
2019 年 5 月更新:
window.orientation
是一项已弃用的功能,大多数浏览器不支持根据 MDN。orientationchange
事件与 window.orientation 关联,因此可能不应该使用。This is what I do:
Update May 2019:
window.orientation
is a deprecated feature and not supported by most browsers according to MDN. Theorientationchange
event is associated with window.orientation and therefore should probably not be used.我使用 window.onresize = function(){ checkOrientation(); }
在 checkOrientation 中,您可以使用 window.orientation 或 body 宽度检查
但想法是,“window.onresize”是最跨浏览器的方法,至少对于我有机会测试的大多数移动和桌面浏览器来说是这样。
I use
window.onresize = function(){ checkOrientation(); }
And in checkOrientation you can employ window.orientation or body width checking
but the idea is, the "window.onresize" is the most cross browser method, at least with the majority of the mobile and desktop browsers that I've had an opportunity to test with.
我对 iOS 和 Phonegap 也很陌生,但我可以通过添加事件监听器来做到这一点。我做了同样的事情(使用您引用的示例),但无法使其工作。但这似乎可以解决问题:
您可能会幸运地搜索 PhoneGap Google Group 术语“方向”。
我读到的关于如何检测方向的一个例子是 Pie Guy:(game ,js 文件)。它与您发布的代码类似,但像您一样......我无法让它工作。
需要注意的是: eventListener 对我有用,但我不确定这是否是一种过于密集的方法。到目前为止,这是唯一对我有用的方法,但我不知道是否有更好、更简化的方法。
更新修复了上面的代码,现在可以使用了
I'm pretty new to iOS and Phonegap as well, but I was able to do this by adding in an eventListener. I did the same thing (using the example you reference), and couldn't get it to work. But this seemed to do the trick:
You may have some luck searching the PhoneGap Google Group for the term "orientation".
One example I read about as an example on how to detect orientation was Pie Guy: (game, js file). It's similar to the code you've posted, but like you... I couldn't get it to work.
One caveat: the eventListener worked for me, but I'm not sure if this is an overly intensive approach. So far it's been the only way that's worked for me, but I don't know if there are better, more streamlined ways.
UPDATE fixed the code above, it works now
虽然这个问题仅涉及 PhoneGap 和 iOS 的使用,并且已经得到解答,但我可以为 2019 年使用 JS 检测屏幕方向的更广泛的问题添加几点:
window.orientation
属性 已弃用,并且 Android 浏览器不支持。有一个较新的属性提供有关方向的更多信息 -screen.orientation
。但它仍处于实验阶段,iOS Safari 不支持。因此,为了获得最佳结果,您可能需要结合使用两者: const angle = screen.orientation ? screen.orientation.angle : window.orientation.正如 @benallansmith 在 他的评论,
window.onorientationchange
事件在window.onresize
之前触发,所以你不会得到屏幕的实际尺寸,除非你在orientationchange事件之后添加一些延迟。有一个 Cordova 屏幕方向插件 用于支持较旧的移动浏览器,但我相信现在没有必要使用它。
还有一个
screen.onorientationchange
事件,但它已弃用,不应使用。添加只是为了答案的完整性。在我的用例中,我不太关心实际方向,而是关心窗口的实际宽度和高度,这显然会随着方向而变化。因此,我使用了
resize
事件来避免处理orientationchange
事件和实现窗口尺寸之间的延迟:注意 1:我在这里使用了 EcmaScript 6 语法,请确保将其编译为 ES5 if需要。
注意2:
window.onresize
事件也会被触发当虚拟键盘切换时,而不仅仅是当方向改变时。Although the question refers to only PhoneGap and iOS usage, and although it was already answered, I can add a few points to the broader question of detecting screen orientation with JS in 2019:
window.orientation
property is deprecated and not supported by Android browsers.There is a newer property that provides more information about the orientation -screen.orientation
. But it is still experimental and not supported by iOS Safari. So to achieve the best result you probably need to use the combination of the two:const angle = screen.orientation ? screen.orientation.angle : window.orientation
.As @benallansmith mentioned in his comment,
window.onorientationchange
event is fired beforewindow.onresize
, so you won't get the actual dimensions of the screen unless you add some delay after the orientationchange event.There is a Cordova Screen Orientation Plugin for supporting older mobile browsers, but I believe there is no need in using it nowadays.
There was also a
screen.onorientationchange
event, but it is deprecated and should not be used. Added just for completeness of the answer.In my use-case, I didn't care much about the actual orientation, but rather about the actual width and height of the window, which obviously changes with orientation. So I used
resize
event to avoid dealing with delays betweenorientationchange
event and actualizing window dimensions:Note 1: I used EcmaScript 6 syntax here, make sure to compile it to ES5 if needed.
Note 2:
window.onresize
event is also fired when virtual keyboard is toggled, not only when orientation changes.在使用
orientationchange
事件时,我需要超时才能获取页面中元素的正确尺寸,但 matchMedia 工作正常。我的最终代码:While working with the
orientationchange
event, I needed a timeout to get the correct dimensions of the elements in the page, but matchMedia worked fine. My final code:我相信正确的答案已经发布并被接受,但是有一个我自己经历过的问题以及其他一些人在这里提到的问题。
在某些平台上,窗口尺寸(
window.innerWidth
、window.innerHeight
)和window.orientation
属性等各种属性将不会更新当事件"orientationchange"
触发时。很多时候,window.orientation
属性在触发"orientationchange"
后的几毫秒内是未定义
(至少在 Chrome 中是这样) iOS)。我发现处理此问题的最佳方法是:
我检查方向是否未定义,或者方向是否等于上次检测到的方向。如果其中一个为真,我会等待十毫秒,然后再次解析方向。如果方向是正确的值,我将调用
showXOrientation
函数。如果方向无效,我会继续循环检查函数,每次等待十毫秒,直到方向有效。现在,我会为此制作一个 JSFiddle,就像我通常做的那样,但是 JSFiddle 并没有为我工作,并且我对它的支持错误已关闭,因为没有其他人报告同样的问题。如果其他人想将其变成 JSFiddle,请继续。
谢谢!我希望这有帮助!
I believe that the correct answer has already been posted and accepted, yet there is an issue that I have experienced myself and that some others have mentioned here.
On certain platforms, various properties such as window dimensions (
window.innerWidth
,window.innerHeight
) and thewindow.orientation
property will not be updated by the time that the event"orientationchange"
has fired. Many times, the propertywindow.orientation
isundefined
for a few milliseconds after the firing of"orientationchange"
(at least it is in Chrome on iOS).The best way that I found to handle this issue was:
I am checking to see if the orientation is either undefined or if the orientation is equal to the last orientation detected. If either is true, I wait ten milliseconds and then parse the orientation again. If the orientation is a proper value, I call the
showXOrientation
functions. If the orientation is invalid, I continue to loop my checking function, waiting ten milliseconds each time, until it is valid.Now, I would make a JSFiddle for this, as I usually did, but JSFiddle has not been working for me and my support bug for it was closed as no one else is reporting the same problem. If anyone else wants to turn this into a JSFiddle, please go ahead.
Thanks! I hope this helps!
这就是我所做的:
here is what i did:
我发现这段代码可以检测设备是否处于横向,在这种情况下添加一个启动页面,显示“更改方向以查看网站”。它适用于 iOS、Android 和 Windows 手机。我认为这非常有用,因为它非常优雅,并且避免为移动网站设置横向视图。
该代码运行良好。唯一不完全令人满意的是,如果有人以横向视图加载页面,则启动页面不会出现。
HTML:
I've found this code to detect if the device is in landscape orientation and in this case add a splash page saying "change orientation to see the site". It's working on iOS, android and windows phones. I think that this is very useful since it's quite elegant and avoid to set a landscape view for the mobile site.
The code is working very well. The only thing not completely satisfying is that if someone load the page being in landscape view the splash page doesn't appears.
And the HTML:
<div id="alert" class="hide"> <div id="content">This site is not thought to be viewed in landscape mode, please turn your device </div> </div>
以下内容对我有用:
我需要超时,因为
window.orientation
的值不会立即更新The following worked for me:
I needed the timeout because the value of
window.orientation
does not update right away我正在 PhoneGap 中为 iPhone 创建 jQTouch 应用程序。我已经和这个问题斗争了好几天了。我已经多次看到建议的事件监听器解决方案,但就是无法让它工作。
最后我想出了一个不同的解决方案。它基本上使用 settimeout 定期检查主体的宽度。如果宽度为 320,则方向为纵向,如果宽度为 480,则方向为横向。然后,如果自上次检查以来方向发生了变化,它将触发纵向填充功能或横向填充功能,您可以在其中为每个方向执行您的操作。
代码(注意,我知道代码中有一些重复,只是还没有费心去修剪它!):
I'm creating a jQTouch app in PhoneGap for the iPhone. I've been battling with this issue for days. I've seen the eventlistener solution suggested a few times, but just could not get it to work.
In the end I came up with a different solution. It basically checks the width of the body periodically using settimeout. If the width is 320 then the orientation is portrait, if 480 then landscape. Then, if the orientation has changed since the last check, it will fire either a portrait stuff function or a landscape stuff function where you can do your thing for each orientation.
Code (note, I know there is some repetition in the code, just haven't bothered to trim it down yet!):