AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > JavaScript

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

51自学网 http://www.wanshiok.com
移动适配

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;

以上所述是小编给大家介绍的移动适配的几种方案(三种方案),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对wanshiok.com网站的支持!


移动适配  
上一篇:javascript中href和replace的比较(详解)  下一篇:利用JS判断字符串是否含有数字与特殊字符的方法小结