rem主要是作用于手机适配使用,创建一个rem.js文件
创建好rem.js文件,然后在需要使用的页面进行引入
//rem.js (function (doc, win) { var isAndroid = win.navigator.appVersion.match(/android/gi); var isIPhone = win.navigator.appVersion.match(/iphone/gi); var scale = 1.0; var ratio = 1; if(isIPhone) { if (window.devicePixelRatio == 2) { scale *= 0.5; ratio *= 2; } if (window.devicePixelRatio == 3) { scale *= (1/3); ratio *= 3; } } var text = '<meta name="viewport" content="initial-scale=' + scale + ', maximum-scale=' + scale + ',' + ' minimum-scale=' + scale + ', width=device-width,' + ' user-scalable=no" />'; document.write(text); var docEl = doc.documentElement var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize' var recalc = function () { var clientWidth = docEl.clientWidth if (!clientWidth) return docEl.style.fontSize = 100 * (clientWidth / 750) + 'px' // 解决部分rem特别大的问题 var docElFontSize = docEl.style.fontSize.replace(/px/gi, '') var computedFontSize = win.getComputedStyle(docEl)['font-size'].replace(/px/gi, '') docElFontSize != computedFontSize && (docEl.style.fontSize = docElFontSize * docElFontSize / computedFontSize + 'px') } if (!doc.addEventListener) return recalc() win.addEventListener(resizeEvt, recalc, false) })(document, window);