Javascript在方向更改后获得移动屏幕宽度

我正在捕获orientationchange事件,如下所示:

  $(window).bind( 'orientationchange', function(e){ }); 

如何在orientationchage之后获得新的屏幕宽度和高度(以像素为单位)?

我已经尝试过screen.width但这并没有改变,它仍然是初始宽度,即使在方向改变之后。

 $(window).bind( 'orientationchange', function(e){ var ori = window.orientation, width = (ori==90 || ori==-90) ? screen.height : screen.width; }); 

使用$(window).resize ,它在orientationchange之后运行

 $(window).resize(function(){ //your logic }); 

如果您正在使用元标记视口,您还可以更新该标记(使用jquery示例:)

  function adapt_to_orientation() { var ori = window.orientation; var screenwidth = (ori==90 || ori==-90) ? screen.height : screen.width; var screenheight = (ori==90 || ori==-90) ? screen.width : screen.height; // resize meta viewport $('meta[name=viewport]').attr('content', 'initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no,width='+screenwidth + ',height=' + screenheight); } adapt_to_orientation(); $(window).bind( 'orientationchange', adapt_to_orientation); 

你也可以使用screen.availWidth 。 它会随着方向而改变。