Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery Mobile Orientation Change Event

jQuery Mobile Orientation Change Event


jQuery Mobile Orientation Change (orientationchange) Event

The orientationchange event is triggered when a user rotates a mobile device vertically or horizontally.

To use the orientationchange event, attach it to the window object:



$(window).on("orientationchange",function(){

    alert("方向有改变!");

});

The callback function can have one parameter, the event object, which returns the orientation of the mobile device: "portrait" (device held vertically) or "landscape" (device held horizontally):

Example

$(window).on("orientationchange",function(event){

alert("The orientation is: " + event.orientation);

});

Try it »

Since the orientationchange event is bound to the window object, we can use the window.orientation property to set different styles to distinguish portrait and landscape views:

Example

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

}

});

Try it »

lamp The window.orientation property returns 0 for portrait view and 90 or -90 for landscape view.