相信不少刚开始写移动端页面的同窗都要面对页面自适应的问题,固然解决方案不少,好比:百分比布局,弹性布局flex(什么是flex),也都能得到不错的效果,这里主要介绍的是本人在实践中用的最顺手最简单的布局方案——rem(什么是rem)布局javascript
rem布局很是简单,首页你只需在页面引入这段原生js代码就能够了css
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if(clientWidth>=640){
docEl.style.fontSize = '100px';
}else{
docEl.style.fontSize = 100 * (clientWidth / 640) + 'px';
}
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);复制代码
这是rem布局的核心代码,这段代码的大意是:
若是页面的宽度超过了640px,那么页面中html的font-size恒为100px,不然,页面中html的font-size的大小为: 100 * (当前页面宽度 / 640)
因而,问题来了,为何要这样?别急,我先来一一回答html
width: 3rem;
height: 2rem;复制代码
那要是宽55px,高37px呢?而后通过换算,,设置以下 ↓width: 2.75rem;
height: 1.85rem;复制代码
是否是发现这换算起来有点麻烦啊,,,(固然,你要是心算帝请无视)width: 0.55rem;
height: 0.37rem;复制代码
是否是换算起来简单多了?!<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<script> (function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function () { var clientWidth = docEl.clientWidth; if (!clientWidth) return; if(clientWidth>=640){ docEl.style.fontSize = '100px'; }else{ docEl.style.fontSize = 100 * (clientWidth / 640) + 'px'; } }; if (!doc.addEventListener) return; win.addEventListener(resizeEvt, recalc, false); doc.addEventListener('DOMContentLoaded', recalc, false); })(document, window); </script>
/*你引进的资源*/
<title>标题</title>
</head>
<body>
/*你的代码*/
</body>
</html>复制代码
Iphone 6 下页面效果前端
Iphone 4 下页面效果java
原文连接:caibaojian.com/rem-respons…git
使用rem实现自适应布局,应该算是当前移动前端的一大趋势,有些人对此还有点迷惑,搞不懂rem是如何实现自适应布局,如何根据设计稿来调整rem的值?rem布局如何用雪碧背景图片?rem必定要加载js吗?rem的根html font-size设置为多少合适?看看这篇文章,也许能帮到你。github
这些问题整理来自以前发表过的文章,细心的读者也能够本身翻翻以前的内容找到答案,本文统一给个回复,若是对你有用,还请点个赞,谢谢!·web
rem是根据html的font-size大小来变化,正是基于这个出发,咱们能够在每个设备下根据设备的宽度设置对应的html字号,从而实现了自适应布局。更多介绍请看这篇文章:rem是如何实现自适应布局的。浏览器
目前有两种,一种是根据js来调整html的字号,另外一种则是经过媒体查询来调整字号。sass
;(function(designWidth, maxWidth) {
var doc = document,
win = window;
var docEl = doc.documentElement;
var tid;
var rootItem,rootStyle;
function refreshRem() {
var width = docEl.getBoundingClientRect().width;
if (!maxWidth) {
maxWidth = 540;
};
if (width > maxWidth) {
width = maxWidth;
}
//与淘宝作法不一样,直接采用简单的rem换算方法1rem=100px
var rem = width * 100 / designWidth;
//兼容UC开始
rootStyle="html{font-size:"+rem+'px !important}';
rootItem = document.getElementById('rootsize') || document.createElement("style");
if(!document.getElementById('rootsize')){
document.getElementsByTagName("head")[0].appendChild(rootItem);
rootItem.id='rootsize';
}
if(rootItem.styleSheet){
rootItem.styleSheet.disabled||(rootItem.styleSheet.cssText=rootStyle)
}else{
try{rootItem.innerHTML=rootStyle}catch(f){rootItem.innerText=rootStyle}
}
//兼容UC结束
docEl.style.fontSize = rem + "px";
};
refreshRem();
win.addEventListener("resize", function() {
clearTimeout(tid); //防止执行两次
tid = setTimeout(refreshRem, 300);
}, false);
win.addEventListener("pageshow", function(e) {
if (e.persisted) { // 浏览器后退的时候从新计算
clearTimeout(tid);
tid = setTimeout(refreshRem, 300);
}
}, false);
if (doc.readyState === "complete") {
doc.body.style.fontSize = "16px";
} else {
doc.addEventListener("DOMContentLoaded", function(e) {
doc.body.style.fontSize = "16px";
}, false);
}
})(640, 640);复制代码
你能够把上面的代码内嵌到html的head中,换算比例是1rem=100px,为了计算方便,你能够在个人一个github项目下载完整的结构和一个压缩的js。
上面代码中的关键代码是:
var width = docEl.getBoundingClientRect().width;
var rem = width * 100 / designWidth;
docEl.style.fontSize = rem + "px";复制代码
说到底,上面的js代码不就是根据不一样的设备调整对应的html字号嘛,那么咱们根据实际的设备,来加上相应的字号不就好了吗?
咱们知道html的默认字号是16px,则对应的设备下能够经过设置对应的font-size使其有一致的缩放比例。
//code from http://caibaojian.com/rem-responsive-2.html
html {
font-size: 62.5%
}
@media only screen and (min-width: 481px) {
html {
font-size:94%!important
}
}
@media only screen and (min-width: 561px) {
html {
font-size:109%!important
}
}
@media only screen and (min-width: 641px) {
html {
font-size:125%!important
}
body {
max-width: 640px
}
}复制代码
上面的代码则是经过1rem=20px来换算的,从哪里看出来呢?从最大的一个值640出发,假如你的设计稿是750的话,那你还要设计更多的媒体查询,换算比例保持一致就能够了。那若是我想要换算比例为1rem=100呢?按照上面的规则,最大的值为:html:font-size:100/16*100%,其它的则按比例,好比480px,则为480/640*(100/16*100%)。
国内的不少人都提倡使用字体图标或者用SVG的方式来实现,但这些在小项目中明显是有点不实际。我一般使用下面两种方式。
结束!