Javascript Orientation更改不适用于iPad上的PhoneGap [英] Javascript Orientation Change doesn't work with PhoneGap on iPad
问题描述
我使用一个小的HTML / CSS / Javascript驱动的网络杂志作为一个iApp的iPad与伟大的移动框架PhoneGap。
I'm adapting a small HTML/CSS/Javascript driven web magazine as an iApp for the iPad with the great mobile framework PhoneGap. Everything works great, except the orientation change from portrait to landscape mode.
我在index.html(portrait)的标题中使用以下Javascript代码:
I use the following Javascript code in the header of the index.html (portrait):
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
function updateOrientation() {
if (isiPad || isiPhone) {
switch (window.orientation) {
case 90:
window.location = "index_landscape.html";
break;
case -90:
window.location = "index_landscape.html";
break;
}
}
}
<body onorientationchange="updateOrientation();">
如果我将iPad旋转到横向模式,它不会改变为index-landscape.html。
If I rotate the iPad to landscape mode, it doesn't change to index-landscape.html.
有人知道问题在哪里吗?
不可能更改HTML文件,而在PhoneGap框架中更改方向?
换句话说,我只能在PhoneGap中使用一个 HTML文件(index.html),并且必须使用CSS来改变方向吗?
Does anybody know where the problem is? Isn't it possible to change the HTML file, while changing orientation within the PhoneGap framework? With other words, can I only use one HTML file (index.html) with PhoneGap and have to use CSS for the orientation change?
如果我使用iPad Safari浏览器将应用程序作为网站签出,则方向更改会正常工作。
If I check out the app as a website with the iPad Safari browser, the orientation change works properly.
感谢任何帮助!
推荐答案
我刚刚找到了另一个解决方案。我使用body onload函数和phonegap.js中的onDeviceReady函数来检查方向:
I just found an other solution to the problem. I use the body onload function with the onDeviceReady function from phonegap.js to check the orientations:
以下javascript代码现在可以正常工作:
The following javascript code works properly now:
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
document.addEventListener("orientationChanged", updateOrientation);
}
function updateOrientation(e) {
switch (e.orientation) {
case 90:
window.location = "index_landscape.html";
break;
case -90:
window.location = "index_landscape.html";
break;
}
}
更新:之前的旧版本的Phonegap和iOS SDK,但根据一些用户,它不工作了。
这篇关于Javascript Orientation更改不适用于iPad上的PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!