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%"});
}
});

嘗試一下 ?

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