19 lines
602 B
JavaScript
19 lines
602 B
JavaScript
|
// 实现rem适配
|
|||
|
(function () {
|
|||
|
var setFont = function () {
|
|||
|
var html = document.documentElement
|
|||
|
// 获取屏幕的宽度
|
|||
|
var width = html.clientWidth
|
|||
|
if (width < 1024) width = 1024
|
|||
|
if (width > 1920) width = 1920
|
|||
|
// 计算当前屏幕下,一个rem所代表的px大小
|
|||
|
var fontSize = width / 80 + 'px'
|
|||
|
console.log(fontSize)
|
|||
|
// 将字体大小赋值给htm根元素,因为rem是参照html根元素的
|
|||
|
html.style.fontSize = fontSize
|
|||
|
}
|
|||
|
setFont()
|
|||
|
window.onresize = function () {
|
|||
|
setFont()
|
|||
|
}
|
|||
|
})()
|