就像PC版的浏览器需要监听窗口缩放事件来作js居中一样,手机版屏幕翻转的时候也需要重新获取窗口宽度。

本来我 原来已经用了bind orientationchange然后if(e.orientation)的方法,可能曾经测试的时候我引用了jquery.mobile吧,后来怎么翻转都alert不出来了。。

然后我把下文中的e去掉,if(window.orientation)就可以了。

监听方法如下:

匿名函数

$(window).bind( 'orientationchange', function(e){
        if(e.orientation){
          if(e.orientation == 'portrait'){
              alert(1);
              }
          else if(e.orientation == 'landscape') {
             alert(2);
            }
        }    
    });

回调函数

$(window).bind('orientationchange',_orientationChange);
function _orientationChange(e){
    
    switch (event.orientation){
            case 90:
            case -90:
                alert(1);
            break;
            case 0:
            case 180:
                alert(2);
            break;
    }
  }

这样分横竖两种情况想干嘛干嘛。

另外jquery mobile 提供了一些手机事件

比如 swipe等等,用的时候可以查查。

posted on 2014-04-25 18:29  meeming  阅读(183)  评论(0编辑  收藏  举报



Fork me on GitHub