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

2. 在入口文件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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-14 09:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-14 09:48:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-14 09:48:01       82 阅读
  4. Python语言-面向对象

    2024-04-14 09:48:01       91 阅读

热门阅读

  1. 数据结构_带头双向循环链表

    2024-04-14 09:48:01       37 阅读
  2. geekos-project3

    2024-04-14 09:48:01       40 阅读
  3. Python常用OS库之path模块学习

    2024-04-14 09:48:01       41 阅读
  4. 【CSS基础】10.过度动画transition和动画animation

    2024-04-14 09:48:01       41 阅读
  5. 前端面试问题汇总 - HTTP篇

    2024-04-14 09:48:01       72 阅读
  6. 常用类——包装类

    2024-04-14 09:48:01       36 阅读
  7. 【leetcode面试经典150题】47. 最长连续序列(C++)

    2024-04-14 09:48:01       30 阅读
  8. Chapter 1-10. Introduction to Congestion in Storage Networks

    2024-04-14 09:48:01       38 阅读