jQuery Mobile 方向事件

jQuery Mobile orientationchange 事件

orientationchange 事件在用户垂直或水平旋转移动设备时被触发。

Mobile


Mobile

如需使用 orientationchange 事件,请把它添加到 window 对象:

  1. $(window).on("orientationchange",function(){
  2. alert("方向已改变!");
  3. });

callback 函数可以设置一个参数,即 event 对象,它会返回移动设备的方向:"portrait" (设备被握持的方向是垂直的)或 "landscape" (设备被握持的方向是水平的):

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

由于 orientationchange 事件与 window 对象绑定,我们能够使用 window.orientation 属性来,例如,设置不同样式以区分 portraitlandscape 视图:

实例
  1. $(window).on("orientationchange",function(){
  2. if(window.orientation == 0) // Portrait
  3. {
  4. $("p").css({"background-color":"yellow","font-size":"300%"});
  5. }
  6. else // Landscape
  7. {
  8. $("p").css({"background-color":"pink","font-size":"200%"});
  9. }
  10. });

提示window.orientation 属性对 portrait 视图返回 0,对 landscape 视图返回 90-90

分类导航