如何在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;
}
3 回答
阿晨1998
TA贡献2037条经验 获得超6个赞
这就是我做的:
function doOnOrientationChange() { switch(window.orientation) { case -90 || 90: alert('landscape'); break; default: alert('portrait'); break; }} window.addEventListener('orientationchange', doOnOrientationChange); // Initial execution if neededdoOnOrientationChange();
根据MDNwindow.orientation
,大多数浏览器都不支持这一功能。该事件与window.orientation相关联,因此可能不应使用。orientationchange
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
我使用window.onresize = function(){ checkOrientation(); }
And在checkOrientation中你可以使用window.orientation或body width检查但是想法是,“window.onresize”是最交叉的浏览器方法,至少对于我已经有的大多数移动和桌面浏览器有机会参加考试。
添加回答
举报
0/150
提交
取消