移动端自适应js

window.addEventListener('resize', setHtmlFontSize)
setHtmlFontSize();

function setHtmlFontSize() {
  var windowWidth = document.documentElement.offsetWidth;
  if (windowWidth > 640) {
    windowWidth = 640;
  } else if (windowWidth < 320) {
    windowWidth = 320;
  }
  var StandardWidth = 375;
  var StandardHtmlFontSize = 16;
  var htmlFontSize = windowWidth / StandardWidth * StandardHtmlFontSize;
  document.querySelector('html').style.fontSize = htmlFontSize + 'px';
}
原文地址:https://www.cnblogs.com/muou2125/p/11356698.html