屏幕旋转事件无法正常工作

3
我正在尝试检测移动设备(iPhone和iPad)的方向更改。
我正在使用以下代码:
 $(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,如上面的链接所述。
请给我提供任何可能的解决方案。
非常感谢您的帮助。
5个回答

2

虽然这个问题比较老,但是以防其他人也遇到同样的问题,你需要使用window.orientation,并且它是用度数表示的:

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

1
我曾遇到同样的问题。以防有人遇到这个问题,请尝试使用window.screen.orientation.type。
jQuery( window ).on( "orientationchange", function( event ) {
    if(window.screen.orientation.type == 'landscape-primary')
    {

    }
    if(window.screen.orientation.type == 'portrait-primary')
    {

    }
});

1

event.orientation并不总是被设置。我在IOS Safari中遇到了这个"undefined"问题。相反,尝试使用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(横屏模式)


0

试试这个:

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

0
$(window).on("orientationchange", function (event) {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接