butterandfly / project_9

Blog & Remark
0 stars 0 forks source link

手机屏幕大小适配 #223

Open butterandfly opened 9 years ago

butterandfly commented 9 years ago
    function viewport() {
      // 检查浏览器,加上相应的meta代码
      var phoneWidth = parseInt(window.screen.width);
      var phoneScale = phoneWidth / 640;
      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, minimum-scale = ' + phoneScale + ', maximum-scale = ' + phoneScale + ', target-densitydpi=device-dpi">');
        } else {
          document.write('<meta name="viewport" content="width=640, target-densitydpi=device-dpi">');
        }
      } else {
        document.write('<meta name="viewport" content="width=640, user-scalable=no, target-densitydpi=device-dpi">');
      }
    }
    viewport();