<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
1.通过css3媒体查询设置font-size
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;
}
}
2.通过js设置font-size
<script>
!function (window) { //来源:http://www.ydui.org/flexible
var dw = 750,
d = window.document,
docEl = d.documentElement,
re = 'orientationchange' in window ? 'orientationchange' : 'resize';
var recalc = (function refreshRem () {
var clientWidth = docEl.getBoundingClientRect().width;
docEl.style.fontSize = Math.max(Math.min(20 * (clientWidth / dw), 11.2), 8.55) * 5 + 'px';
/*说明: 8.55:小于320px不再缩小,11.2:大于420px不再放大, 17.067 :大于640px不再放大*/
return refreshRem;
})();
docEl.setAttribute('data-dpr', window.navigator.appVersion.match(/iphone/gi) ? window.devicePixelRatio : 1);/* 添加倍屏标识,安卓为1 */
if (/iP(hone|od|ad)/.test(window.navigator.userAgent)) {
d.documentElement.classList.add('ios'); /* 添加IOS标识 */
if (parseInt(window.navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/)[1], 10) >= 8) /* IOS8以上给html添加hairline样式,以便特殊处理 */
d.documentElement.classList.add('hairline');
}
if (!d.addEventListener) return;
window.addEventListener(re, recalc, false);
d.addEventListener('DOMContentLoaded', recalc, false);
}(window);
</script>