我正在这样捕获orientationchange
事件:
$(window).bind( 'orientationchange', function(e){
});
如何在 orientationchange
后获取新的屏幕宽度和高度(以像素为单位)?
我尝试过使用screen.width
,但这不会改变,即使在方向更改后仍然保持最初的宽度。
我正在这样捕获orientationchange
事件:
$(window).bind( 'orientationchange', function(e){
});
如何在 orientationchange
后获取新的屏幕宽度和高度(以像素为单位)?
我尝试过使用screen.width
,但这不会改变,即使在方向更改后仍然保持最初的宽度。
$(window).bind( 'orientationchange', function(e){
var ori = window.orientation,
width = (ori==90 || ori==-90) ? screen.height : screen.width;
});
width = Math.abs(window.orientation) == 90 ? screen.height : screen.width;
这至少可以节省分配变量和对同一变量的双重评估。(但是在我第一次看到这个问题(在移动设备上)并发表评论之间,使用三元运算符+1。) - David Thomasori
变量的唯一原因是为了避免代码行过长。 - Blazemonger你也可以使用 screen.availWidth
。它会随着屏幕方向而改变。
使用 $(window).resize,它会在orientationchange之后运行
$(window).resize(function(){
//your logic
});
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);