码迷,mamicode.com
首页 > Web开发 > 详细

web网站在不同设备下进行缩放

时间:2018-06-07 17:52:23      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:function   oat   list   phone   cto   script   rtm   lis   sel   

<script>
function calculatescale() {
var phoneWidth = parseInt(window.screen.width);
var phoneScale = phoneWidth / 1280;//除以的值按手机的物理分辨率
var ua = navigator.userAgent;
var viewportmetacontent = "";
if (/Android (\d+\.\d+)/.test(ua)) {
var version = parseFloat(RegExp.$1);
if (version > 2.3) { // andriod 2.3以上
viewportmetacontent=‘width=device-width,initial-scale=‘ + phoneScale + ‘,minimum-scale=‘ + phoneScale + ‘,user-scalable=yes‘;
} else {
viewportmetacontent=‘width=device-width,user-scalable=yes‘;
}
// 其他系统
} else {
viewportmetacontent = ‘width=device-width, initial-scale=‘ + phoneScale + ‘,minimum-scale=‘ + phoneScale + ‘,user-scalable=yes‘;
}
return viewportmetacontent;
}
document.write(‘<meta name="viewport" content="‘+calculatescale()+‘">‘);
</script>

 

window.addEventListener(‘orientationchange‘, function (event) {
var viewportmeta = document.querySelector(‘meta[name="viewport"]‘);
if (viewportmeta) {
viewportmeta.content = calculatescale();
}
});

web网站在不同设备下进行缩放

标签:function   oat   list   phone   cto   script   rtm   lis   sel   

原文地址:https://www.cnblogs.com/cathyxiao/p/9151704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!