解决前端笔记本电脑屏幕显示缩放比例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

相关推荐
leafyyuki几秒前
从零到一落地「智能助手」:一次基于 OpenSpec 的流式对话前端实践
前端·vue.js·人工智能
踩着两条虫2 分钟前
VTJ:架构设计模式
前端·架构·ai编程
孙凯亮3 分钟前
Three.js VR 模拟器(Immersive Web Emulator)踩坑全记录:从报错到可用,避坑指南一次性奉上
前端·three.js
CDN3603 分钟前
2026年Web性能优化实测:360CDN如何通过“时效性”与“地域性”双杀提升排名?
前端·性能优化
Dxy123931021612 分钟前
Python使用XPath定位元素:and和or组合条件
前端·javascript·python
驱动小百科13 分钟前
笔记本电脑USB接口没反应怎么办 2026最新修复指南
电脑·笔记本usb接口没反应·usb接口失灵怎么办·电脑不识别u盘·电脑usb接口失效
李剑一17 分钟前
可以说99%的前端都没咋用过!JS逗号操作符,面试常考但业务少用?一篇吃透不踩坑
前端
百结21418 分钟前
HAProxy 搭建 Web 集群
前端·web
GISer_Jing23 分钟前
Todos
前端·人工智能·学习