首页 > 关于移动端rem适配

关于移动端rem适配

var num = 1 / window.devicePixelRatio;
var fontSize = document.documentElement.clientWidth / 10;
document.getElementsByTagName('html')[0].style.fontSize = fontSize + 'px';

适配移动端rem单位,实际使用的时候用量取到的像素值/75即为计算后的rem值,标准为iPhone6的750的尺寸。750px的设计稿的标准宽度为10rem;

补充另一种,本质不变

!function (a, d) {function c() {var t = document.documentElement.clientWidth,i = document.documentElement.clientHeight;d.documentElement.style.fontSize = ((t / i) >= 0.75) ? t * 0.1 + "px" : t / 750 * 75 + "px"};var b = null;a.addEventListener("onorientationchange" in a ? "orientationchange" : "resize", function () {clearTimeout(b);b = setTimeout(c, 300)}, !1);a.addEventListener("pageshow", function (a) {a.persisted && (clearTimeout(b), b = setTimeout(c, 300))}, !1);d.addEventListener("DOMContentLoaded", c, false)
}(window, document);

 

转载于:https://www.cnblogs.com/DreamerLeaf/p/9667589.html

更多相关: