移動端rem設置(部分安卓機型不兼容)
阿新 • • 發佈:2018-01-23
settime body 安卓 屏幕 gpo color ref rect() refresh
(function(win) { var doc = win.document; var docEl = doc.documentElement; var tid; function refreshRem() { var width = docEl.getBoundingClientRect().width; if (width > 640) { // 最大寬度 width = 640; } var rem = width / 10; // 將屏幕寬度分成10份, 1份為1rem docEl.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(); console.log(docEl.style.fontSize) })(window);
移動端rem設置(部分安卓機型不兼容)