移动端适配

// 设置 rem 函数
function setRem() {
  // 750 设计图
  const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
  // 得到html的Dom元素
  const htmlDom = document.getElementsByTagName('html')[0]
  // 设置根元素字体大小
  if (htmlWidth >= 640) {
    htmlDom.style.fontSize = '100px'
  } else {
    // eslint-disable-next-line prefer-template
    htmlDom.style.fontSize = 100 * (htmlWidth / 750) + 'px'
  }
}
// 初始化
setRem()
// 改变窗口大小时重新设置 rem
window.onresize = function resize() {
  setRem()
}
原文地址:https://www.cnblogs.com/wsj1/p/14149827.html