javascript Javascript在方向改变后获取移动屏幕宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13200135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript get mobile screen width after orientation change
提问by ServerBloke
I am capturing the orientationchange
event like this:
我正在捕获这样的orientationchange
事件:
$(window).bind( 'orientationchange', function(e){
});
How can I get the new screen width and height in pixels after the orientationchage
?
如何在orientationchage
?之后获得新的屏幕宽度和高度(以像素为单位)?
I have tried screen.width
but this doesn't change, it remains as the initial width, even after orientation change.
我试过了,screen.width
但这并没有改变,它仍然是初始宽度,即使在方向改变之后也是如此。
回答by Blazemonger
$(window).bind( 'orientationchange', function(e){
var ori = window.orientation,
width = (ori==90 || ori==-90) ? screen.height : screen.width;
});
回答by Sarim
Also you can use screen.availWidth
. It gets changed with orientation.
你也可以使用screen.availWidth
. 它会随着方向而改变。
回答by Alain Gauthier
Use $(window).resize, it runs after orientationchange
使用$(window).resize,它在orientationchange后运行
$(window).resize(function(){
//your logic
});
回答by Air2
If you are using the meta tag viewport you could also update that tag (with jquery example:)
如果您正在使用元标记视口,您还可以更新该标记(使用 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);