【问题标题】:orientationchange event is not working方向更改事件不起作用
【发布时间】:2012-06-19 15:08:16
【问题描述】:

我正在尝试检测移动设备、iphone 和 ipad 中的方向变化。

我正在使用以下代码:

 $(window).bind("orientationchange", function (event) {
    alert(event.orientation);

    position = $.event.special.orientationchange.orientation();

    event.preventDefault();
});

这里的警报值,即 event.orientation 显示为“未定义”,正如我在一些帖子中读到的那样,它确实支持检测方向。 同样在 jquery 文档中,它被编写为更好地使用“event.orientation”而不是“window.orientation”,但这些都没有返回所需的结果,都返回“未定义”。

所以我使用了“$.event.special.orientationchange.orientation();”检测方向。

但我想将 jquery 文档中定义的功能用作 event.orientation。如link

另外,在我的代码中,“$.mobile.orientationChangeEnabled”设置为 true,如上面的链接所示。

请为此建议我任何可能的解决方案。

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: jquery-mobile orientation screen-orientation


    【解决方案1】:

    老问题,但以防万一其他人遇到它,您必须使用 window.orientation,并且以度数表示:

    portrait: 0
    portrait (upside down): 180 
    landscape: (counterclockwise): 90
    landscape: (clockwise): -90
    

    【讨论】:

      【解决方案2】:

      event.orientation 并不总是设置。我在 IOS Safari 中遇到了这个“未定义”的问题。相反,请尝试使用 window.orientation:

      //trigger a button click when orientation is changed to landscape mode
      $(window).on('orientationchange', function(event) {
          if (window.orientation == 90 || window.orientation == -90) {
              $('.close-button').click();
          }
      });
      

      window.orientation 可以是 0 或 180(纵向模式),或 90 或 -90(横向模式)

      【讨论】:

        【解决方案3】:

        我有同样的问题。以防万一有人遇到这个问题。 试试 window.screen.orientation.type

        jQuery( window ).on( "orientationchange", function( event ) {
            if(window.screen.orientation.type == 'landscape-primary')
            {
        
            }
            if(window.screen.orientation.type == 'portrait-primary')
            {
        
            }
        });
        

        【讨论】:

          【解决方案4】:

          试试这个:

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

          【讨论】:

            【解决方案5】:
            $(window).on("orientationchange", function (event) {
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-09-23
              • 2016-07-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-10
              • 2016-02-25
              相关资源
              最近更新 更多