页面尺寸等比例缩放

发布于:2023-10-25 ⋅ 阅读:(52) ⋅ 点赞:(0)

  1. 页面尺寸等比例缩放
<script>
  // 环境开发: width: 375 fontSize: 1rem = 100px
  // 获取设备宽度
  var width = document.documentElement.clientWidth || document.body.clientWidth
  var ratio = width / 375
  var fontSize = 100 * ratio
  document.getElementsByClassName('html')[0].style['font-size'] = fontSize + 'px'
</script>
// 实时更新
(function flexible(window, document) {
  function resetFontSize() {
    const size = (document.documentElement.clientWidth / 1920) * 37.5;
    document.documentElement.style.fontSize = size + 'px';
  }

  // reset root font size on page show or resize
  window.addEventListener('pageshow', resetFontSize);
  window.addEventListener('resize', resetFontSize);
})(window, document);