移动端rem适配function

移动端rem适配js // 默认375,750设计稿请将375替换为750 (function (doc, win) { // 移动端适配 var docEl = doc.documentElement, isIOS = navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), dpr = isIOS ? Math.min(win.devicePixelRatio, 3) : 1, dpr = window.top === window.self ? dpr : 1, //被iframe引用时,禁止缩放 dpr = 1, scale = 1 / dpr, resizeEvt = "orientationchange" in window ? "orientationchange" : "resize"; docEl.dataset.dpr = dpr; var metaEl = doc.createElement("meta"); metaEl.name = "viewport"; metaEl.content = "initial-scale=" + scale + ",maximum-scale=" + scale + ", minimum-scale=" + scale; docEl.firstElementChild.appendChild(metaEl); var recalc = function () { var width = docEl.clientWidth; if (width / dpr > 375) { width = 375 * dpr; } // 乘以100,px : rem = 100 : 1 docEl.style.fontSize = 100 * (width / 375) + "px"; }; recalc(); if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false); })(document, window); html兼容样式 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,minimal-ui,viewport-fit=cover" /> <meta content="black-translucent" /> <meta content="black" /> <meta content="yes" /> <meta content="telephone=no" /> <meta content="yes" /> <meta content="email=no" /> <meta content="portrait" /> <meta content="portrait" /> <meta content="true" /> <meta content="yes" /> <meta content="webkit" /> <title>Title</title> </head> <body> <div></div> </body> </html> CSS样式

建议在 body 中设置全局默认字体大小

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwyywp.html