解决前端笔记本电脑屏幕显示缩放比例125%、150%对页面大小的影响问题--数据可视化大屏

近期在工作中遇到一个问题,记录一下,在项目上线之后,遇到一个问题,即缩放到90%时,页面字体比默认的100%字体大,一开始毫无头绪,经过一番的Google...Google...Google...,终于找到了解决方法,这是因为大多数笔记本电脑默认的缩放比例为125%或者是150%,所以就出现了在本身台式电脑(默认100%)上开发出来的页面都是按照100%比例来开发的,之后在笔记本电脑上打开缩放比例的时候会出现字体大小显示不合理的问题,这种问题主要是因为device-pixel-ratio导致的

原文链接:https://blog.csdn.net/m0_46318298/article/details/133786669

1.新建一个js文件

复制代码
// detectZoom.js
export const 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
}
  1. 在入口文件main.js引入

    import { detectZoom } from '@/utils/detectZoom';
    // 处理笔记本系统默认系统比例为125%或150%带来的布局影响
    const m = detectZoom();
    document.body.style.zoom = 100 / Number(m);

题外--数据可视化大屏实现技术方案 vue和react的

vue + v-scale-screen组件 + dataV组件

react + r-scale-screen + dataV

相关推荐
Bigger19 分钟前
🔥 一份 Agent 工程岗 JD,暴露了市场真正想要什么样的人
前端·agent·全栈
我头上有犄角ovo23 分钟前
我在微信小程序里手搓人脸识别引导,结果被“右转头”和“手遮脸”教育了
前端
David_Xia23 分钟前
干爆 11s 提交卡顿!引入 Rust 级 oxlint 彻底拯救团队 Git Commit 噩梦的重构实践
前端
前端环境观察室24 分钟前
别急着让 Agent 跑任务,先把浏览器环境上下文建模
前端
蝎子莱莱爱打怪40 分钟前
零基础用AI写App?兄弟😂 醒醒吧,那只是个玩具罢了!
前端·人工智能·后端
用户1306095607231 小时前
elpis里程碑一的阶段性总结
前端
砍材农夫1 小时前
物联网 基于netty控制报文结构(发布与接收)
java·开发语言·前端·javascript·物联网
光影少年1 小时前
react的Context 跨层传值、优缺点、适用场景
前端·react.js·掘金·金石计划
kevinten101 小时前
说实话,我做了个"不务正业"的 AI:专门推荐冷门冒险地
前端
上单带刀不带妹1 小时前
Vue3 中 getCurrentInstance() 与 proxy 详解
前端·javascript·vue.js