字体自适应屏幕大小变化(抄来的)


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM