Last active
June 26, 2018 03:20
-
-
Save shengoo/601c594c8a7819caba723ee63f760c9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* @param {Number} [baseFontSize = 100] - 基础fontSize, 默认100px; | |
* @param {Number} [fontscale = 1] - 有的业务希望能放大一定比例的字体; | |
*/ | |
const win = window; | |
export default win.flex = (baseFontSize, fontscale) => { | |
const _baseFontSize = baseFontSize || 100; | |
const _fontscale = 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},viewport-fit=cover`); | |
doc.documentElement.style.fontSize = `${_baseFontSize / 2 * dpr * _fontscale}px`; | |
}; | |
// 开始设置,可以根据业务需求修改参数 | |
flex(100, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment