2012-01-13 30 views
5

我正在爲Android和iPhone製作網絡應用程序。我的問題是,我想刷新網頁,當用戶去風景。由於我是JavaScript的初學者,我不知道該怎麼做。請幫我使用方法當用戶將手機變爲風景時刷新網頁

+2

存在着這樣和答案的問題;) http://stackoverflow.com/questions/850472/how-do-i-detect-when-the- iphone-goes-into-landscape-mode-via-javascript-is- – matzino 2012-01-13 09:52:07

+0

@ManseUK感謝U – FreshBits 2012-01-13 10:01:43

+0

請關閉此問題以避免重複 – Murtaza 2012-01-13 10:12:21

回答

5

您可以使用orientationchange jQuery Mobile的事件,然後做一個

window.location.reload(); 

刷新頁面。

例如:

$(function(){ 

// Set Inital orientation 
// get the initial orientation from window which 
// returns 0 for portrait and 1 for landscape 
if(window.orientation == 0){ 
    var ori = "portrait"; 
}else{ 
    var ori = "landscape"; 
} 
changeOrientation(ori); 

// Orientation Change 
// When orientation changes event is triggered 
// exposing an orientation property of either 
// landscape or portrait 
$('body').bind('orientationchange',function(event){ 
    changeOrientation(event.orientation) 
}) 

// Change the style dependengt on orientation 
function changeOrientation(ori){ 
    // Remove all classes separated by spaces 
    $("#orientation").removeClass('portrait landscape'); 
    $("#orientation").addClass(ori); 
    $("#orientation").html("<p>"+ori.toUpperCase()+"</p>"); 
} 

}); 
+0

我用過這樣的代碼,仍然不工作「」 – FreshBits 2012-01-13 09:59:35

+0

@yuzi我已經更新了我的代碼,但我建議你看看我n到jquery和jQuery的手機(這個例子是使用) – Johan 2012-01-13 10:03:38

+1

終於我做到了這一點, 然後它的工作 「window.onorientationchange = function(){ var orientation = window.orientation; 開關(方向){ case 0: window.location.reload(); 休息; case 90: window.location.reload(); 休息;案例-90: window.location.reload(); 休息; } }「 thanks Johan – FreshBits 2012-01-14 04:00:24

0

你可以使用一個計時器並定期檢查:

if (window.innerHeight > window.innerWidth) { 
    // Redirect code, window.location... 
} 
+2

如果高度>寬度,手機將處於肖像模式,而不是風景 – Johan 2012-01-13 09:50:59

相關問題