Latest web development tutorials

jQuery Mobile 方向改變事件

jQuery Mobile 方向改變(orientationchange)事件

當用戶垂直或水平旋轉移動設備時,觸發方向改變(orientationchange)事件。





Mobile


如需使用方向改變(orientationchange)事件,請附加它到window 對象:

$(window).on("orientationchange",function(){
	alert("方向有改变!");
});

回調函數可有一個參數,event 對象,返回移動設備的方向:"縱向"(設備保持在垂直位置)或"橫向"(設備保持在水平位置):

實例

$(window).on("orientationchange",function(event){
alert("方向是: " + event.orientation);
});

嘗試一下»

由於方向改變(orientationchange)事件綁定到window 對象,我們可以使用window.orientation 屬性來設置不同的樣式,以便區分縱向和橫向的視圖:

實例

$(window).on("orientationchange",function(){
if(window.orientation == 0) // Portrait
{
$("p").css({"background-color":"yellow","font-size":"300%"});
}
else // Landscape
{
$("p").css({"background-color":"pink","font-size":"200%"});
}
});

嘗試一下»

lamp window.orientation 屬性針對縱向視圖返回0,針對橫向視圖返回90 或-90。