首页 > 技术文章 > 字体自适应屏幕大小变化(抄来的)

wilsunson 2019-11-07 15:36 原文

 <script>
      (function(win) {
        var doc = win.document
        var docEl = doc.documentElement
        var tid
        doc.body.addEventListener('touchstart', function () {})

        function refreshRem() {
          var width = docEl.getBoundingClientRect().width
          if (width > 750) { // 最大宽度
            width = 750
          }
          var rem = width / 7.5
          docEl.style.fontSize = rem + 'px'
          // 开发实际尺寸为:750设计稿的尺寸 / 100 rem
        }

        win.addEventListener('resize', function() {
          clearTimeout(tid);
          tid = setTimeout(refreshRem, 300)
        }, false);
        win.addEventListener('pageshow', function(e) {
          if (e.persisted) {
            clearTimeout(tid)
            tid = setTimeout(refreshRem, 300)
          }
        }, false)
        refreshRem()
      })(window)
    </script>

使用:.app{font-size: 0.24rem;}

推荐阅读