[英]Javascript get mobile screen width after orientation change
我正在捕获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;
});
您也可以使用screen.availWidth
。 它随着方向而改变。
使用$(窗口).resize,它orientationchange后运行
$(window).resize(function(){
//your logic
});
如果您使用的是meta标签视口,则还可以更新该标签(使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.