Silence-dream / Silence-dream.github.io

blog博客
https://silence-dream.github.io
3 stars 0 forks source link

移动端 DPI 适配 #11

Open Silence-dream opened 1 year ago

Silence-dream commented 1 year ago
    var phoneWidth = parseInt(window.screen.width);
    var phoneScale = phoneWidth / 750;
    var ua = navigator.userAgent;
    if (/Android (\d+\.\d+)/.test(ua)) {
      var version = parseFloat(RegExp.$1);
      // andriod 2.3
      if (version > 2.3) {
        document.write('<meta name="viewport" content="width=750, minimum-scale = ' + phoneScale +
          ', maximum-scale = ' + phoneScale + ', target-densitydpi=device-dpi">');
        // andriod 2.3
      } else {
        document.write('<meta name="viewport" content="width=750, target-densitydpi=device-dpi">');
      }
    } else {
      document.write('<meta name="viewport" content="width=750, user-scalable=no, target-densitydpi=device-dpi">');
    }

    var deviceWidth = document.documentElement.clientWidth;
    if (deviceWidth > 750) {
      deviceWidth = 750
    };
    document.documentElement.style.fontSize = deviceWidth / 750 + 'px';