orientationchange事件jQuery mobile

时间:2013-08-13 23:40:01

标签: jquery jquery-mobile orientation-changes

好的,这是我的代码。

$( window ).on( "orientationchange", function( event ) {
alert('1');
if (event.orientation == 'landscape'){
    alert('2');
    $('.item').css('width', '30.66%');   
} else if (event.orientation == 'portrait') {
    $('.item').css('width', '47%');   
}
});

$(document).on('ready', function () {
    $( window ).orientationchange(); 
});

我只是想改变名为.item的对象的宽度。我在jQuery mobile上查找了orientationchange的语法,看起来是正确的。现在,每当我按预期更改方向时,我都会收到第一个警报,但我没有进入if语句。从我读到的内容中,您可以通过event.orientation访问方向,它等于“肖像”或“风景”,但它似乎对我不起作用。 最后一部分只是为了在页面加载时调用该函数,它始终正常工作。感谢

1 个答案:

答案 0 :(得分:0)

Orientation是window.screen对象的属性,而不是事件。试试

if (window.screen.orientation.indexOf('landscape') >= 0){...}

由于这是一个新兴标准,您可能还需要检查供应商特定的实施,例如window.screen.mozOrientation

<强>参考