手机适应横屏竖屏问题
<pre name="code" class="javascript">function screenFixed(){
var agent = navigator.userAgent.toLowerCase();
//适配安卓大屏手机
if(agent.indexOf('android') > -1 && agent.indexOf('mobi') > -1){
if(window.devicePixelRatio > 1){
//这里有个坑,用screen.width计算,小米手机,横屏,依然跟竖屏一样的值
//而且用innerWidth计算,不用多除以 dpi的值 window.screen.width/(320*window.devicePixelRatio)
if(agent.indexOf('miuibrowser') > -1){ //小米手机强制使用innerWidth计算
var zoom = window.innerWidth / (320) * 100;
}else{
var zoom = window.screen.width/(320*window.devicePixelRatio) *100;
}
zoom = (zoom>100) ? zoom : window.innerWidth/320*100;
$('html').css({
zoom: zoom+'%'
});
}
}
//适配安卓大屏pad,这里有个坑,安卓部分pad设备agent没有android标识,跟ipad一样的agent
if((agent.indexOf('macintosh') > -1 || agent.indexOf('ipad') > -1) && window.devicePixelRatio == 1){
var zoom = window.innerWidth / (320) * 100;
//var zoom = window.screen.width/(320*window.devicePixelRatio) *100;
zoom = (zoom>100) ? zoom : window.innerWidth/320*100;
$('html').css({
zoom: zoom+'%'
});
}
}
function addEvent(){
//监听屏幕翻转事件
var orientationEvent = ('onorientationchange' in window) ? 'orientationchange' : 'resize';
window.addEventListener(orientationEvent, function() {
$('html').css('visibility','hidden');
window.setTimeout(function(){
screenFix();
$('html').css('visibility','visible');
},1000);
}, false);
}