-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path页面自适应rem兼容华为机型
33 lines (33 loc) · 1.12 KB
/
页面自适应rem兼容华为机型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// /**
// * 通过rem实现页面自适应
// */
(function (doc, win) {
var docEl = doc.documentElement
var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
var recalc = function () {
var clientWidth = docEl.clientWidth
if (!clientWidth) return
if (clientWidth >= 769) {
docEl.style.fontSize = '100px'
} else {
docEl.style.fontSize = 100 * (clientWidth / 375) + 'px'
}
var html = document.getElementsByTagName('html')[0]
var settedFs = parseInt(100 * (clientWidth / 375))
var settingFs = settedFs
var whileCount = 0
while (true) {
var realFs = parseInt(window.getComputedStyle(html).fontSize)
var delta = realFs - settedFs
//不相等
if (Math.abs(delta) >= 1) {
if (delta > 0) { settingFs-- } else { settingFs++ }
html.setAttribute('style', 'font-size:' + settingFs + 'px!important')
} else { break }
if (whileCount++ > 100) { break }
}
}
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false)
doc.addEventListener('DOMContentLoaded', recalc, false)
})(document, window);