2012-06-19 118 views
3

我试图检测移动设备,iPhone和iPad的方向更改。orientationchange事件不起作用

我使用下面的代码:

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

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

    event.preventDefault(); 
}); 

这里戒备即event.orientation的值显示为“未定义”,其中正如我在一些后已阅读,它以检测取向支持。 另外在jQuery文档中,它被写入更好地使用“event.orientation”而不是“window.orientation”,但没有一个返回所需的结果,都返回“undefined”。

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

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

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

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

任何帮助,将不胜感激。

在此先感谢。

回答

0

试试这个:

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

老问题,但以防万一别人遇到它,你必须使用window.orientation,那就是以度表示:

portrait: 0 
portrait (upside down): 180 
landscape: (counterclockwise): 90 
landscape: (clockwise): -90 
0
$(window).on("orientationchange", function (event) { 
0

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(横向模式)