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

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

解决办法:

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
}

2)在入口文件main.js中引入

复制代码
import { detectZoom } from '@/utils/detectZoom';
// 处理笔记本系统默认系统比例为125%或150%带来的布局影响
const m = detectZoom();
document.body.style.zoom = 100 / Number(m);
相关推荐
共享家952719 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
摘星编程21 小时前
OpenHarmony环境下React Native:自定义useTruncate文本截断
javascript·react native·react.js
Duang007_1 天前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
有来技术1 天前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5161 天前
学院个人信息管理系统 (springboot+vue)
vue.js·spring boot·后端·个人开发·毕设
2601_949868361 天前
Flutter for OpenHarmony 电子合同签署App实战 - 主入口实现
开发语言·javascript·flutter
m0_748229991 天前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
xiaoxue..1 天前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试
摘星编程1 天前
在OpenHarmony上用React Native:自定义useHighlight关键词高亮
javascript·react native·react.js
2601_949613021 天前
flutter_for_openharmony家庭药箱管理app实战+用药知识详情实现
android·javascript·flutter