移动端布局:
rem方案:页面中的任何元素都采用rem布局,包括字体。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
width: 7.5rem;
font-size: 32px;
font-size: .32rem;
max-width: 1080px;
margin: 0 auto;
background: #f6f6f6;
font-family: 'stheiti', 'microsoft yahei', helvetica, arial, sans-serif;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
</style>
</head>
<body>
<script type="text/javascript">
var dpr = 1,
uagent = window.navigator.useragent;
var isios = uagent.match(/iphone/i);
var isyixin = uagent.match(/yixin/i);
var is2345 = uagent.match(/mb2345/i);
var ishaosou = uagent.match(/mso_app/i);
var issogou = uagent.match(/sogoumobilebrowser/ig);
var isliebao = uagent.match(/liebaofast/i);
var isgnbr = uagent.match(/gnbr/i);
function resizeroot() {
var wwidth = (screen.width > 0) ? (window.innerwidth >= screen.width || window.innerwidth == 0) ? screen.width :
window.innerwidth : window.innerwidth,
wdpr, wfsize;
var wheight = (screen.height > 0) ? (window.innerheight >= screen.height || window.innerheight == 0) ?
screen.height : window.innerheight : window.innerheight;
if(window.devicepixelratio) {
wdpr = window.devicepixelratio;
} else {
wdpr = isios ? wwidth > 818 ? 3 : wwidth > 480 ? 2 : 1 : 1;
}
if(isios) {
wwidth = screen.width;
wheight = screen.height;
}
if(wwidth > wheight) {
wwidth = wheight;
}
wfsize = wwidth > 1080 ? 144 : wwidth / 7.5;
wfsize = wfsize > 32 ? wfsize : 32;
window.screenwidth_ = wwidth;
if(isyixin || is2345 || ishaosou || issogou || isliebao || isgnbr) { //yixin 和 2345 这里有个刚调用系统浏览器时候的bug,需要一点延迟来获取
settimeout(function() {
wwidth = (screen.width > 0) ? (window.innerwidth >= screen.width || window.innerwidth == 0) ?
screen.width : window.innerwidth : window.innerwidth;
wheight = (screen.height > 0) ? (window.innerheight >= screen.height || window.innerheight ==
0) ? screen.height : window.innerheight : window.innerheight;
wfsize = wwidth > 1080 ? 144 : wwidth / 7.5;
wfsize = wfsize > 32 ? wfsize : 32;
document.getelementsbytagname('html')[0].style.fontsize = wfsize + 'px';
document.getelementbyid("fixed").style.display = "none";
}, 500);
} else {
document.getelementsbytagname('html')[0].style.fontsize = wfsize + 'px';
document.getelementbyid("fixed").style.display = "none";
}
}
resizeroot();
</script>
</body>
</html>
html的font-size大小是根据移动设备自动计算的。
设计师完成的设计稿宽度为:750px,此时html跟节点的大小是50px,其他元素根据这个基准值设置rem大小。
页面上html的font-size不是预先通过媒介查询在css里定义好的,而是通过js计算出来的,如上述代码中的js代码部分就是计算html的font-size的大小。
以上就是分析网易移动端布局实例方案的详细内容。