适配浏览器缩放比例
1 class DevicePixelRatio { 2 constructor() { } 3 getSystem() { 4 var agent = navigator.userAgent.toLowerCase(); 5 var ismac = /macintosh|mac os x/i.test(navigator.userAgent); 6 if (ismac) { 7 return false; 8 } 9 if (agent.indexOf('windows') >= 0) { 10 return true; 11 } 12 } 13 correct() { 14 var ratio = 0, 15 screen = window.screen, 16 ua = navigator.userAgent.toLowerCase(); 17 18 if (window.devicePixelRatio !== undefined) { 19 ratio = window.devicePixelRatio; 20 } 21 else if (~ua.indexOf('msie')) { 22 if (screen.deviceXDPI && screen.logicalXDPI) { 23 ratio = screen.deviceXDPI / screen.logicalXDPI; 24 } 25 } 26 else if (window.outerWidth !== undefined && window.innerWidth !== undefined) { 27 ratio = window.outerWidth / window.innerWidth; 28 } 29 30 document.getElementsByTagName('body')[0].style.zoom = 1 / ratio; 31 } 32 // 监听页面缩放 33 watch() { 34 window.addEventListener('resize', this.correct); 35 } 36 unWatch() { 37 window.removeEventListener('resize', this.correct); 38 } 39 // 初始化页面比例 40 init() { 41 if (this.getSystem()) { 42 this.correct(); 43 this.watch(); 44 } 45 } 46 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律