'use strict'; /** * @param {Number} [baseFontSize = 100] - 基礎fontSize, 默認100px; * @param {Number} [fontscale = 1] - 有的業務但願能放大必定比例的字體; */ const win = window; const _baseFontSize = 150; const _fontscale = 1; const doc = win.document; const ua = navigator.userAgent; const matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i); const UCversion = ua.match(/U3\/((\d+|\.){5,})/i); const isUCHd = UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80; const isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi); let dpr = win.devicePixelRatio || 1; if (!isIos && !(matches && matches[1] > 534) && !isUCHd) { // 若是非iOS, 非Android4.3以上, 非UC內核, 就不執行高清, dpr設爲1; dpr = 1; } const scale = 1 / dpr; let metaEl = doc.querySelector('meta[name="viewport"]'); if (!metaEl) { metaEl = doc.createElement('meta'); metaEl.setAttribute('name', 'viewport'); doc.head.appendChild(metaEl); } metaEl.setAttribute('content', 'width=device-width,user-scalable=no,initial-scale=' + scale +',maximum-scale=' + scale + ',minimum-scale=' + scale); doc.documentElement.style.fontSize = `${_baseFontSize / 2 * dpr * _fontscale}px`;
在html插入這段代碼,而後在樣式文件使用base字體爲75px的基準轉換px到rem便可。必要時還能夠經過vh,vw調整。html
https://www.jianshu.com/p/e96ccb603a50app