原生Js 实现等比缩放页面

针对1920*1080 分配率缩放

window.addEventListener('load', adaptation);
window.addEventListener('resize', adaptation);
function adaptation () {
  var w = document.body.clientWidth;
  var h = document.body.clientHeight;
  var nw = 1920;
      nh = 1080;
  var left, top, scale;
  if (w / h > nw / nh) {
    scale = h / nh;
    top = 0;
    left = (w - nw * scale) / 2;
  } else {
    scale = w / nw;
    left = 0;
    top = (h - nh * scale) / 2;
  }
  document.getElementById('main').setAttribute('style', 'transform: scale('+ scale +');left:'+left+'px;top:'+top+'px;');
}

转载于:https://www.cnblogs.com/yaohe/p/10843353.html

你可能感兴趣的:(原生Js 实现等比缩放页面)