UNPKG

783 BJavaScriptView Raw
1/**
2 * rem响应式方案的实现
3 * Created by liuzhengdong on 2018/4/3.
4 */
5import { throttle } from './StringUtils'
6import appConfig from '@cwd/app.config'
7
8const win = global
9const doc = win.document
10const baseWidth = appConfig.designSize || 750 // 设计稿尺寸
11const optimalSize = appConfig.optimalSize || 414 // 最优尺寸
12const documentHTML = doc.documentElement
13
14/**
15 * 设置html根字体
16 */
17function setRootFont() {
18 const docWidth = documentHTML.getBoundingClientRect().width
19 const scale = (docWidth > 720 ? optimalSize : docWidth) / baseWidth
20 documentHTML.style.fontSize = `${scale * 100}px`
21 documentHTML.setAttribute('data-dpr', global.devicePixelRatio === 3 ? 3 : 2)
22}
23
24setRootFont()
25win.addEventListener('resize', throttle(setRootFont, 90, 100), false)
26