移动端rem布局方法
750px的psd设计稿,
1、自适应@media设置,取值x/20 rem;
@media only screen and (min-width: 241px) and (max-width: 320px) {
html {
font-size: 8.53px !important
}
}
@media only screen and (min-width: 321px) and (max-width: 360px) {
html {
font-size: 9.6px !important
}
}
@media only screen and (min-width: 361px) and (max-width: 375px) {
html {
font-size: 10px !important
}
}
@media only screen and (min-width: 376px) and (max-width: 384px) {
html {
font-size: 10.24px !important
}
}
@media only screen and (min-width: 385px) and (max-width: 414px) {
html {
font-size: 11.04px !important
}
}
@media only screen and (min-width: 415px) and (max-width: 480px) {
html {
font-size: 12.8px !important
}
}
@media only screen and (min-width: 481px) and (max-width: 600px) {
html {
font-size: 13px !important
}
}
@media screen and (min-width: 601px) {
html {
font-size: 14px !important
}
}
2、淘宝,取值x/75rem
<script>
!function (a, b) {
function c() {
var b = f.getBoundingClientRect().width;
b / i > 540 && (b = 540 * i);
var c = b / 10;
f.style.fontSize = c + "px", k.rem = a.rem = c
}
var d, e = a.document, f = e.documentElement, g = e.querySelector('meta[name="viewport"]'), h = e.querySelector('meta[name="flexible"]'), i = 0, j = 0, k = b.flexible || (b.flexible = {});
if (g) {
console.warn("将根据已有的meta标签来设置缩放比例");
var l = g.getAttribute("content").match(/initial\-scale=([\d\.]+)/);
l && (j = parseFloat(l[1]), i = parseInt(1 / j))
} else if (h) {
var m = h.getAttribute("content");
if (m) {
var n = m.match(/initial\-dpr=([\d\.]+)/), o = m.match(/maximum\-dpr=([\d\.]+)/);
n && (i = parseFloat(n[1]), j = parseFloat((1 / i).toFixed(2))), o && (i = parseFloat(o[1]), j = parseFloat((1 / i).toFixed(2)))
}
}
if (!i && !j) {
var p = a.navigator.userAgent, q = (!!p.match(/android/gi), !!p.match(/iphone/gi)), r = q && !!p.match(/OS 9_3/), s = a.devicePixelRatio;
i = q && !r ? s >= 3 && (!i || i >= 3) ? 3 : s >= 2 && (!i || i >= 2) ? 2 : 1 : 1, j = 1 / i
}
if (f.setAttribute("data-dpr", i), !g)if (g = e.createElement("meta"), g.setAttribute("name", "viewport"), g.setAttribute("content", "initial-scale=" + j + ", maximum-scale=" + j + ", minimum-scale=" + j + ", user-scalable=no"), f.firstElementChild) f.firstElementChild.appendChild(g); else {
var t = e.createElement("div");
t.appendChild(g), e.write(t.innerHTML)
}
a.addEventListener("resize", function () {
clearTimeout(d), d = setTimeout(c, 300)
}, !1), a.addEventListener("pageshow", function (a) {
a.persisted && (clearTimeout(d), d = setTimeout(c, 300))
}, !1), "complete" === e.readyState ? e.body.style.fontSize = 12 * i + "px" : e.addEventListener("DOMContentLoaded", function () {
e.body.style.fontSize = 12 * i + "px"
}, !1), c(), k.dpr = a.dpr = i, k.refreshRem = c, k.rem2px = function (a) {
var b = parseFloat(a) * this.rem;
return "string" == typeof a && a.match(/rem$/) && (b += "px"), b
}, k.px2rem = function (a) {
var b = parseFloat(a) / this.rem;
return "string" == typeof a && a.match(/px$/) && (b += "rem"), b
}
}(window, window.lib || (window.lib = {}));
</script>

浙公网安备 33010602011771号