jquery mobile在电话差距中的方向改变事件实现

Nis*_*ant 23 jquery jquery-mobile cordova

有人可以让我知道jquery mobile在电话差距中的方向更改事件的正确代码吗?我在哪里以及如何实现此orientationChange函数?

gho*_*der 45

$(window).bind('orientationchange', _orientationHandler);
Run Code Online (Sandbox Code Playgroud)

然后在_orientationHandler函数中,有类似的东西:

if(event.orientation){
      if(event.orientation == 'portrait'){
                  //do something
      }
      else if(event.orientation == 'landscape') {
                    //do something
      }
}
Run Code Online (Sandbox Code Playgroud)

  • 当前的jQuery版本推荐`on`事件处理程序:`$(window).on("orientationchange",function(event){}` (4认同)
  • 无论你想做什么改变方向. (3认同)

小智 12

$(window).bind( 'orientationchange', function(e){
    if ($.event.special.orientationchange.orientation() == "portrait") {
        //Do whatever in portrait mode
    } else {
        //Do Whatever in landscape mode
    }
});
Run Code Online (Sandbox Code Playgroud)

如果您的目标是iOS并且orientationchange不起作用,您可以在bind函数的event参数中添加resize事件.由于更改方向也会触发调整大小事件.

  • 无论您打算做什么,因为手机处于肖像模式或横向模式. (3认同)