当前位置: 首页 > news >正文

不侵入代码的rem适配,支持桌面缩放,vue2的适配方案,包含echarts适配

此方式不侵入代码,自动把px单位转换成rem单位

首先安装postcss-pxtorem@5.1.1

yarn add postcss-pxtorem@5.1.1
npm install postcss-pxtorem@5.1.1 --save

项目根目录新建 postcss.config.js

module.exports = {plugins: {'postcss-pxtorem': {rootValue: 14,propList: ['*'],},},
};

utils目录下新建rem.js和detectZoom.js并引入main.js中,引入方式

...
import '@/utils/rem'
import { detectZoom } from '@/utils/detectZoom';
const m = detectZoom();
document.body.style.zoom = 100 / Number(m);
...

detectZoom.js

export const detectZoom = () => {let ratio = 0,screen = window.screen,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 * 90);}return ratio;
};

rem.js

const baseSize = 14; //跟postcss.config.js中rootValue的值是一致的
// 设置 rem 函数
function setRem() {// // 当前页面宽度相对于 1920 宽的缩放比例,可根据自己需要修改。const scale = document.documentElement.clientWidth / 1920;// // 设置页面根节点字体大小 最高为两倍图 即设计稿为750document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + "px";
}
// 初始化
setRem();
// 改变窗口大小时重新设置 rem
window.onresize = function () {setRem();
};

echarts字体适配方法

/***根据分辨率大小缩放echarts的字体大小* @param {*} res* @returns*/
export function fontSize(res) {let docEl = document.documentElement,clientWidth =window.innerWidth ||document.documentElement.clientWidth ||document.body.clientWidth;if (!clientWidth) return;let fontSize = 1 * (clientWidth / 1920);return res * fontSize;
}

已知问题

  •  detectZoom.js引入后echarts图表会错位不受控,不引入即可。
http://www.lryc.cn/news/119331.html

相关文章:

  • 智能合约 -- 常规漏洞分析 + 实例
  • JavaScript 操作历史记录api怎样使用 JavaScript
  • Spring 容器
  • 【腾讯云Cloud Studio实战训练营】使用React快速构建点餐H5
  • Java培训课程哪个品牌好?快拿小本本记好
  • leetcode19. 删除链表的倒数第 N 个结点
  • c51单片机串行通信示例代码(单片机--单片机通信)(附带proteus线路图)
  • UML之四种事物
  • 盒子模型和新盒子模型及区别
  • 移动端Vue组件库-vant
  • Java课题笔记~ JSP内置对象
  • 数据结构笔记--链表经典高频题
  • Android Ble蓝牙App(三)特性和属性
  • 日常BUG——使用Long类型作id,后端返回给前段后精度丢失问题
  • 【C++初阶】string类的常见基本使用
  • 【ArcGIS Pro二次开发】(60):按图层导出布局
  • docker-desktop数据目录迁移
  • 03.利用Redis实现缓存功能---解决缓存穿透版
  • 全景图!最近20年,自然语言处理领域的发展
  • Mybatis参数传递
  • 手动实现 Spring 底层机制 实现任务阶段一编写自己 Spring 容器-准备篇【2】
  • 部署模型并与 TVM 集成
  • Android Navigation 导航切换fragment用法
  • Anaconda Prompt使用pip安装PyQt5-tools后无法打开Spyder或闪退
  • 【jvm】jvm整体结构(hotspot)
  • 通达信波段选股公式,使用钱德动量摆动指标(CMO)
  • 家电维修小程序开发指南:从零搭建到上线
  • 玩赚音视频开发高阶技术——FFmpeg
  • python 变量赋值 修改之后 原值改变
  • 拂袖一挥,zipfile秒列zip包内容