devicePixelRatio.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * @Author: code4eat awesomedema@gmail.com
  3. * @Date: 2023-02-03 10:52:22
  4. * @LastEditors: code4eat awesomedema@gmail.com
  5. * @LastEditTime: 2023-02-03 10:52:38
  6. * @FilePath: /BudgetManaSystem/src/utils/devicePixelRatio.js
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. */
  9. class DevicePixelRatio {
  10. constructor() {
  11. //this.flag = false;
  12. }
  13. //获取系统类型
  14. _getSystem() {
  15. let flag = false;
  16. var agent = navigator.userAgent.toLowerCase();
  17. // var isMac = /macintosh|mac os x/i.test(navigator.userAgent);
  18. // if(isMac) {
  19. // return false;
  20. // }
  21. //现只针对windows处理,其它系统暂无该情况,如有,继续在此添加
  22. if(agent.indexOf("windows") >= 0) {
  23. return true;
  24. }
  25. }
  26. //获取页面缩放比例
  27. // _getDevicePixelRatio() {
  28. // let t = this;
  29. // }
  30. //监听方法兼容写法
  31. _addHandler(element, type, handler) {
  32. if(element.addEventListener) {
  33. element.addEventListener(type, handler, false);
  34. } else if(element.attachEvent) {
  35. element.attachEvent("on" + type, handler);
  36. } else {
  37. element["on" + type] = handler;
  38. }
  39. }
  40. //校正浏览器缩放比例
  41. _correct() {
  42. let t = this;
  43. //页面devicePixelRatio(设备像素比例)变化后,计算页面body标签zoom修改其大小,来抵消devicePixelRatio带来的变化。
  44. document.getElementsByTagName('body')[0].style.zoom = 1 / window.devicePixelRatio;
  45. }
  46. //监听页面缩放
  47. _watch() {
  48. let t = this;
  49. t._addHandler(window, 'resize', function() { //注意这个方法是解决全局有两个window.resize
  50. //重新校正
  51. t._correct()
  52. })
  53. }
  54. //初始化页面比例
  55. init() {
  56. let t = this;
  57. if(t._getSystem()) { //判断设备,目前只在windows系统下校正浏览器缩放比例
  58. //初始化页面校正浏览器缩放比例
  59. t._correct();
  60. //开启监听页面缩放
  61. t._watch();
  62. }
  63. }
  64. }
  65. export default DevicePixelRatio;