人到中年有點(diǎn)甜
2019-08-15 15:39:13
如何在iOS上使用Phonegap正確檢測(cè)方向變化?我在下面找到了這個(gè)方向測(cè)試代碼,尋找JQTouch參考資料。這在移動(dòng)Safari上的iOS模擬器中正常工作,但在Phonegap中無(wú)法正確處理。我的項(xiàng)目遇到了與殺死此測(cè)試頁(yè)面相同的問(wèn)題。有沒(méi)有辦法在Phonegap中使用JavaScript來(lái)感知方向變化?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貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊
這就是我做的:
function doOnOrientationChange() { switch(window.orientation) { case -90 || 90: alert('landscape'); break; default: alert('portrait'); break; }} window.addEventListener('orientationchange', doOnOrientationChange); // Initial execution if neededdoOnOrientationChange();
根據(jù)MDNwindow.orientation
,大多數(shù)瀏覽器都不支持這一功能。該事件與window.orientation相關(guān)聯(lián),因此可能不應(yīng)使用。orientationchange

海綿寶寶撒
TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
我使用window.onresize = function(){ checkOrientation(); }
And在checkOrientation中你可以使用window.orientation或body width檢查但是想法是,“window.onresize”是最交叉的瀏覽器方法,至少對(duì)于我已經(jīng)有的大多數(shù)移動(dòng)和桌面瀏覽器有機(jī)會(huì)參加考試。
添加回答
舉報(bào)
0/150
提交
取消