首页 > 其他分享 >使用js获取系统分辨率、系统缩放倍数和浏览器缩放倍数

使用js获取系统分辨率、系统缩放倍数和浏览器缩放倍数

时间:2024-02-01 15:46:33浏览次数:24  
标签:浏览器 缩放 zoom js window 倍数 const

做屏幕适配让人头大,用rem适配需要获取到系统缩放倍数浏览器缩放倍数来计算根节点字体大小,网上找来找去都没看见一个满意的方案,自己折腾一个算是一个比较完美的方案吧,亲测谷歌浏览器120版本有效

// 获取缩放倍数(1*系统缩放倍数*浏览器缩放倍数)
function getZoom() {
  let zoom = 1;
  const screen = window.screen,
    ua = navigator.userAgent.toLowerCase();

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

const getDecimal = (num) => {
  return Math.round(num * 100) / 100;
};

function getAllZoom() {
  // 总缩放倍数
  const zoom = getZoom();
  // 屏幕分辨率
  const screenResolution = window.screen.width;
  // 获取浏览器内部宽度
  const browserWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  // 浏览器缩放倍数
  // 浏览器外部宽度不受浏览器缩放影响,浏览器内部宽度受影响,所以根据这个可以计算出浏览器缩放倍数(F12调试工具的占位会影响该值)
  const browserZoom = getDecimal(window.outerWidth / browserWidth);
  // 系统缩放倍数
  const systemZoom = getDecimal(zoom / browserZoom);
  // 系统分辨率
  const systemResolution = Math.round(screenResolution * systemZoom);

  console.log('系统分辨率', systemResolution, '屏幕分辨率', screenResolution, '浏览器外部宽度', window.outerWidth, '浏览器内部宽度', browserWidth, '总缩放倍数', zoom, '浏览器缩放倍数', browserZoom, '系统缩放倍数', systemZoom);

  return {
    zoom,
    browserZoom,
    systemZoom,
    systemResolution
  }
}

getAllZoom();

标签:浏览器,缩放,zoom,js,window,倍数,const
From: https://www.cnblogs.com/mlzs/p/18001400

相关文章

  • Python | json文件和jsonl文件
    Python|json文件和jsonl文件JSON文件是使用JSON(JavaScriptObjectNotation)格式存储数据的文件。它是一种结构化的文本格式,使用键值对的方式表示数据。JSON文件通常包含一个根对象,可以包含多个嵌套的对象、数组和基本数据类型。而JSONL文件(JSONLines),则是一种每行包含一......
  • 跑通的第一个ethers.js程序HelloVitalik.js
    简介ethers.js是一个本地库,可以让你调用接口,用官方写好的轮子来使用一些常用的函数!学习完这个库,你对node.js就有比较深入的了解了,如果你不做项目,就不涉及智能合约的编写,那么写点脚本学习一下ethers.js是很好的。教程已经有比较完整系统的了:https://www.wtf.academy/ethers-101/H......
  • nvm安装Nodejs时报错,Could not retrieve https://npm.taobao.org/mirrors/node/latest
    1.首先要使用管理员运行命令2.在安装nvm的目录下找到settings.txt,没有就手动增加一个node_mirror:https://npm.taobao.org/mirrors/node/npm_mirror:https://npm.taobao.org/mirrors/npm/这个地方有点奇怪,安装18的时候把上面的Https://去掉以后就下载成功了3.安装19以及......
  • js获取/设置css变量
    -<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title><st......
  • js处理excel数据
    借助xlsx实现js读取处理excel数据,并输出excel文件<inputtype="file"@change="fn"/>import*asXLSXfrom'xlsx'fn(e){letfileObj=e.target.files[0]constfileReader=newFileReader()fileReader.readAsArrayBuffer(fileObj)fileR......
  • jmeter Regular Expression Extractor和JSON Extractor使用
    在Jmete中我们经常遇到需要把当前接口返回内容用于下一个接口请求,应用场景:登录接口返回的cookie或者token等,下面就简单描述一下两种提取器的使用和需要注意的事项:1、RegularExpressionExtractorApplyto:待研究;Fieldtocheck:取值的地方,ReferenceName:取得值后存放值的变量......
  • js使用map
     js使用map  getData(){      //调用接口方法      getlistInspectionnum().then(response=>{        console.log("######234#######")        //console.log(response.length)       ......
  • 解决前端JSZip读取压缩包内文件中文名称乱码问题
    1importJSZipfrom"jszip";23functionjszipFile(file){4if(!/\.zip$/i.test(file.name)){5reject(`文件"${file.name}"不是zip文件`);6return;7}89constfileReader=newFileReader();1011//转换文件为Arra......
  • 12.旋转、缩放、倾斜、平移Transform
    RotateTransform旋转RotateTransform表示旋转一个对象的角度。首先我们来看一下它的定义publicsealedclassRotateTransform:Transform{publicstaticreadonlyDependencyPropertyAngleProperty;publicstaticreadonlyDependencyPropertyCenterXPropert......
  • 2024年1月份更新「GIS数据」全国的GeoJSON、shp格式数据下载获取(精确到乡镇街道级)
    发现个可以免费下载全国 geojson 数据的网站,推荐一下。支持全国、省级、市级、区/县级、街道/乡镇级以及各级的联动数据,支持导入矢量地图渲染框架中使用,例如:D3、Echarts等geojson数据下载地址:https://geojson.hxkj.vip该项目github地址:https://github.com/TangSY/echarts-m......