移动端字体自适应大小

抄来的

<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;}

 

 
 
posted @ 2021-01-29 15:56  chchchc  阅读(543)  评论(0编辑  收藏  举报