PC端
( function ( win ) {
var tid;
function refreshRem ( ) {
let designSize = 1920 ;
let html = document. documentElement;
let wW = html. clientWidth;
let rem = wW * 100 / designSize;
document. documentElement. style. fontSize = rem + 'px' ;
}
win. addEventListener ( 'resize' , function ( ) {
clearTimeout ( tid) ;
tid = setTimeout ( refreshRem, 300 ) ;
} , false ) ;
win. addEventListener ( 'pageshow' , function ( e ) {
if ( e. persisted) {
clearTimeout ( tid) ;
tid = setTimeout ( refreshRem, 300 ) ;
}
} , false ) ;
refreshRem ( ) ;
} ) ( window) ;
移动端
( function ( win, doc ) {
if ( ! win. addEventListener) return ;
var html = document. documentElement;
function setFont ( ) {
var html = document. documentElement;
var k = 750 ;
html. style. fontSize = html. clientWidth / k * 100 + "px" ;
}
setFont ( ) ;
setTimeout ( function ( ) {
setFont ( ) ;
} , 300 ) ;
doc. addEventListener ( 'DOMContentLoaded' , setFont, false ) ;
win. addEventListener ( 'resize' , setFont, false ) ;
win. addEventListener ( 'load' , setFont, false ) ;
} ) ( window, document) ;