您好,欢迎访问一九零五行业门户网

移动适配的几种方案(三种方案)

1.直接利用js适配
(function (doc, win) { var docel = doc.documentelement, resizeevt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function () { var clientwidth = docel.clientwidth; if (!clientwidth) return; docel.style.fontsize = 100 * (clientwidth / 750) + 'px'; }; if (!doc.addeventlistener) return; win.addeventlistener(resizeevt, recalc, false); doc.addeventlistener('domcontentloaded', recalc, false); })(document, window);
例如:100px=1rem;10px=0.1rem;1px=0.01rem;
2.利用js+less适配
(function (win) { function setunita() { document.documentelement.style.fontsize = document.documentelement.clientwidth / 10 + "px"; } var h = null; window.addeventlistener("resize", function () { cleartimeout(h); h = settimeout(setunita, 300); }, false); setunita(); })(window);
less:文件顶部定义@unit: 750/10rem,然后css全文件的单位直接用@unit。
例如:100px=100/@unit;10px=10/@unit;1px=1/@unit;
3.利用less适配。
html { font-size: 20px; } @media only screen and (min-width: 401px) { html { font-size: 25px !important; } } @media only screen and (min-width: 428px) { html { font-size: 26.75px !important; } } @media only screen and (min-width: 481px) { html { font-size: 30px !important; } } @media only screen and (min-width: 569px) { html { font-size: 35px !important; } } @media only screen and (min-width: 641px) { html { font-size: 40px !important; } } @unit: 40rem;
例如:100px=100/@unit;10px=10/@unit;1px=1/@unit;
其它类似信息

推荐信息