获取当前设备的屏幕宽度 发表于 2018-01-30 | 分类于 学习笔记 , 技术 , 前端 | 阅读次数: 123456789101112131415161718192021222324252627<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"> <title>获取当前设备的屏幕宽度</title> <script> var deviceWidth = parseInt(window.screen.width); //获取当前设备的屏幕宽度 var deviceScale = deviceWidth / 640; //得到当前设备屏幕与640之间的比例,之后我们就可以将网页宽度固定为640px var ua = navigator.userAgent; //获取当前设备类型(安卓或苹果) if (/Android (\d+\.\d+)/.test(ua)) { var version = parseFloat(RegExp.$1); if (version > 2.3) { document.write('<meta name="viewport" content="width=640,initial-scale=' + deviceScale + ', minimum-scale = ' + deviceScale + ', maximum-scale = ' + deviceScale + ', target-densitydpi=device-dpi">'); } else { document.write('<meta name="viewport" content="width=640,initial-scale=0.75,maximum-scale=0.75,minimum-scale=0.75,target-densitydpi=device-dpi" />'); } } else { document.write('<meta name="viewport" content="width=640, user-scalable=no">'); } </script></head><body></body></html> 本文作者: 简兮 本文链接: http://www.secretbase.vip/获取当前设备的屏幕宽度.html 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!