获取和设置浏览器缩放比

解决 电脑默认缩放比不是100%的情况
 let zoom = window.devicePixelRatio;
  document.body.style.zoom = 1/zoom;
/**
 * 获取浏览器缩放百分比
 */
 function getZoom() {
  var ratio = 0,
      screen = window.screen,
      ua = navigator.userAgent.toLowerCase();

  if (window.devicePixelRatio !== undefined) {
    ratio = window.devicePixelRatio;
  }
  else if (~ua.indexOf('msie')) {
    if (screen.deviceXDPI && screen.logicalXDPI) {
      ratio = screen.deviceXDPI / screen.logicalXDPI;
    }
  }
  else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
    ratio = window.outerWidth / window.innerWidth;
  }

  if (ratio) {
    ratio = Math.round(ratio * 100);
  }
  return ratio;
}

 

posted @ 2021-09-23 16:24  JS-Feng  阅读(1032)  评论(0编辑  收藏  举报