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

如需使用方向改變(orientationchange)事件,請附加它到 window 對象:
$(window).on("orientationchange",function(){ alert("方向有改變!"); });
回調函數可有一個參數,event 對象,返回移動設備的方向:"縱向"(設備保持在垂直位置)或"橫向"(設備保持在水準位置):
實例
$(window).on("orientationchange",function(event){
alert("方向是: " + event.orientation);
});
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%"});
}
});
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。 |
---|