window.onresize = function(){ updateOrientation(e); }
function updateOrientation(e) {
alert("hey");
deviceWidth = $('body').width();
if (deviceWidth == 320) {
currentOrientation = "portrait";
}
else if (deviceWidth == 480) {
currentOrientation = "landscape";
}
// fire a function that checks the orientation every x milliseconds
setInterval(checkOrientation, 500);
// check orientation
function checkOrientation() {
deviceWidth = $('body').width();
if (deviceWidth >= '1200') {
newOrientation = "portrait";
}
else if (deviceWidth <= '900') {
newOrientation = "landscape";
}
// if orientation changed since last check, fire either the portrait or landscape function
if (newOrientation != currentOrientation) {
if (newOrientation == "portrait") {
changedToPortrait();
}
else if (newOrientation == "landscape") {
changedToLandscape();
}
currentOrientation = newOrientation;
}
}
// landscape stuff
function changedToLandscape() {
alert('Orientation has changed to Landscape!');
}
// portrait stuff
function changedToPortrait() {
alert('Orientation has changed to Portrait!');
}
}
我正在使用它来检测Android平板电脑使用phonegap的方向变化。不知何故,代码无效。有什么建议?我猜测无法生成方向更改事件。
答案 0 :(得分:0)
这适用于PhoneGap:
function onOrientationChange() {
switch (window.orientation) {
case -90:
case 90:
alert('landscape');
break;
default:
alert('portrait');
break;
}
};
window.onresize = onOrientationChange;