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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
(function(win) { let time const doc = document const baseSize = 100 const designWidth = 375 const maxWidth = 750 let width = doc.documentElement.getBoundingClientRect().width const height = doc.documentElement.clientHeight
let screenVertical = true if (width <= height) { screenVertical = true } else { screenVertical = false }
function setRem() { if (width > maxWidth) { width = maxWidth } const rem = screenVertical ? width * baseSize / designWidth : height * baseSize / designWidth doc.documentElement.style.fontSize = rem + 'px' }
win.addEventListener('resize', function() { clearTimeout(time) time = setTimeout(setRem, 300) }, false)
win.addEventListener('pageshow', function(e) { if (e.persisted) { clearTimeout(time) time = setTimeout(setRem, 300) } }, false)
setRem() })(window)
|