页面响应式rem

·

1 min read

function handleResize() {
    var htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
    let htmlDom = document.getElementsByTagName('html')[0]
    // 处理不同页面宽度下rem的值
    if (htmlWidth < 1400 && htmlWidth > 1000) {
      htmlDom.style.fontSize = 12 / 1920 * htmlWidth + 'px';
    } else if (htmlWidth < 1000) {
      htmlDom.style.fontSize = 7 + 'px';
    } else {
      // 默认 1rem = 10px
      htmlDom.style.fontSize = 10 / 1920 * htmlWidth + 'px';
    }
  }
  handleResize();
  window.onresize = handleResize;