«

js获取浏览器和电脑的放大倍数

emer 发布于 2021-6-24 16:24   2128 次阅读     


如果我们可以通过js获取到电脑的默认缩放,那对我们做页面适配将会有很大的帮助。下面就跟大家分享下js代码:

function detectZoom() {
      let ratio = 0
      const screen = window.screen
      const 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

},

计算后,使用zoom将页面缩小