之前写过一篇博客,内容也是关于移动端手机页面适配的《MobileWeb适配之路》,个人感觉这篇文章介绍的方法比本篇介绍的方法的限制要少,比如没有强制设计宽度为640/750之类,BUG也比较的少,至于选择哪种方法那是仁者见仁,智者见智个人选择的问题,最主要还是看实际项目工作的需求来选择方法。
以下介绍的内容来源:http://www.jianshu.com/p/985d26b40199
这里做简要概述如何使用和注意的地方,首先是使用方法
不要在页面中添加viewport,让下面的代码自动生成
在源码的head中引入下面的代码即可:
<script> !function(e){function t(a){if(i[a])return i[a].exports; var n=i[a]={exports:{},id:a,loaded:!1}; return e[a].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var i={}; return t.m=e,t.c=i,t.p="",t(0)} ([function(e,t){"use strict"; Object.defineProperty(t,"__esModule",{value:!0}); var i=window; t["default"]=i.flex=function(e,t){var a=e||100,n=t||1,r=i.document,o=navigator.userAgent,d=o.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i),l=o.match(/U3\/((\d+|\.){5,})/i),c=l&&parseInt(l[1].split(".").join(""),10)>=80,p=navigator.appVersion.match(/(iphone|ipad|ipod)/gi),s=i.devicePixelRatio||1;p||d&&d[1]>534||c||(s=1); var u=1/s,m=r.querySelector('meta[name="viewport"]');m||(m=r.createElement("meta"),m.setAttribute("name","viewport"),r.head.appendChild(m)),m.setAttribute("content","width=device-width,user-scalable=no,initial-scale="+u+",maximum-scale="+u+",minimum-scale="+u),r.documentElement.style.fontSize=a/2*s*n+"px"},e.exports=t["default"]}]); flex(100, 1);</script>
原理请看原文介绍。
需要注意的地方:
除了前面提到的不手动写viewport之外
需要设计稿宽度为640/750
该方案的比例是1rem=100px,转换的时候需要注意
元素宽度大于640/750的一半,也就在设计稿640的时候宽度大于320,设计稿750的时候大于375,则元素使用百分比宽度,类似等宽屏图片宽度100%
在微信浏览器中字体大小不受限制的解决方法,给所有元素设置一个max-height,比如100000px
html,body,div,span,h1,h2,h3,h4,h5,h6...{ ... max-height:100000px; /* 解决微信浏览器字体不受限制的BUG*/ }