flexible佈局是淘寶團隊開發移動版時爲了適配各類大小不一的手機屏幕採用的方案。css
如今的手機換代速度真是讓咱們這些開發人員汗顏啊。要作的適配實在是太多太蛋疼了。而本人蔘加工做接到的第一個任務就是作一個頁面要求適配全部的手機設備,在PC端瀏覽的時候要像微信文章同樣兩邊留空固定頁面大小,這個不是重點。重點是適配全部手機設備!!!對於當時的我真是一臉懵逼,當時真是花了九牛二虎之力啊。最後也是勉勉強強地完成了任務。html
最近無心中看到了淘寶的H5手機端適配方案——flexible。發現當時本身作的真是弱爆了。決定本身嘗試一下這個神奇的適配方案。作上一個小例子記錄下來,若是往後忘了的時候也方便再次查看。android
首先在頁面加載前要先加載flexible.js文件web
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="js/flexible.js"></script>複製代碼
頁面元素佈局代碼bash
<div class="container">
<div class="top-bar">
<div class="top-bar-left">
<div>用戶名稱</div>
<div class="line">
<div class="orLine"></div>
</div>
<div>開放平臺</div>
</div>
<div class="enter">進入PC端</div>
</div>
<div class="img-container">
<div class="img">
<img src="img/h1 copy@3x.png">
</div>
<div class="img">
<img src="img/h2 copy@3x.png">
</div>
<div class="img">
<img src="img/h3 copy@3x.png">
</div>
<div class="img">
<img src="img/h4 copy@3x.png">
</div>
<div class="img">
<img src="img/h5 copy@3x.png">
</div>
<div class="img">
<img src="img/h6 copy@3x.png">
</div>
<div class="img">
<img src="img/h7 copy@3x.png">
</div>
<div class="img">
<img src="img/h8 copy@3x.png">
</div>
<div class="img">
<img src="img/h9 copy@3x.png">
</div>
<div class="img">
<img src="img/h10 copy@3x.png">
</div>
</div>
</div>
複製代碼
css樣式設置微信
body {
display: flex;
justify-content: center;
}
.container {
width: 100%;
}
.top-bar {
font-size: .4rem /* 15/37.5 */;
height: 1.47rem /* 55/37.5 */;
opacity: 0.8;
background: #000;
display: flex;
justify-content: space-between;
align-items: center;
color: #fff;
font-family: 'PingFangSC-Regular';
}
/* 根據需求
如要求字體不跟隨屏幕大小變化而變化就使用此方式
全部的字體大小設置都使用px
而不是使用rem做爲單位
[data-dpr="2"] .top-bar {
font-size: 30px;
}
[data-dpr="3"] .top-bar {
font-size: 45px;
}*/
.top-bar .top-bar-left {
display: flex;
justify-content: flex-start;
margin-left: .4rem /* 15/37.5 */;
}
.top-bar .top-bar-left .line {
display: flex;
align-items: center;
}
.top-bar .top-bar-left .line .orLine {
width: 0.75px;
height: .35rem /* 13.15/37.5 */;
background: #fff;
margin: 0 .21rem /* 8/37.5 */ 0 .21rem /* 8/37.5 */;
}
.top-bar .enter {
margin: 0 .4rem /* 15/37.5 */ 0 0;
}
img {
width: 100%;
height: auto;
}
.img-container {
padding: 0 .72rem /* 27/37.5 */ 0 .72rem /* 27/37.5 */;
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
}
.img {
width: 2.53rem /* 95/37.5 */;
height: 1.09rem /* 41/37.5 */;
margin: .4rem /* 15/37.5 */ 0 0 0;
}
.img-container > div:nth-child(2),.img-container > div:nth-child(5),.img-container > div:nth-child(8) {
margin-left: .48rem /* 18/37.5 */;
margin-right: .48rem /* 18/37.5 */;
}
@media (min-width: 640px) {
.container {
width: 640px;
}
}
複製代碼
最後效果app
iphone5iphone
iphone6ide
PC端佈局
能夠看到不管屏幕多大,都能完美的適配。
該方案實現的方式是根據屏幕的大小設置1rem = ?px,通常會把1rem設置爲(屏幕寬度/10)px,flexible就是這樣處理。頁面元素使用rem做爲大小單位,而後根據不一樣屏幕的dpr(devicePixelRatio)對頁面元素進行縮放。達到一個比較完美的自適應效果。
ps:一般字體大小是不使用rem做爲單位,由於若是字體也是使用rem做爲單位的話在實際生產中字體就會隨屏幕大小而改變。因此,根據實際需求區別處理。
flexiable爲咱們提供了data-dpr這一屬性使得咱們能夠在不一樣dpr的屏幕保持字體的大小不改變
[data-dpr="2"] .top-bar {
font-size: 30px;
}
[data-dpr="3"] .top-bar {
font-size: 45px;
}
複製代碼
注意:flexible只爲iphone作了高清的適配,對於安卓設備,統一採用dpr = 1的設置。
flexible詳細代碼以下:
! function () {
var a = "@charset \"utf-8\";html{color:#000;background:#fff;overflow-y:scroll;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}html *{outline:0;-webkit-text-size-adjust:none;-webkit-tap-highlight-color:rgba(0,0,0,0)}html,body{font-family:sans-serif}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td,hr,button,article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{margin:0;padding:0}input,select,textarea{font-size:100%}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}abbr,acronym{border:0;font-variant:normal}del{text-decoration:line-through}address,caption,cite,code,dfn,em,th,var{font-style:normal;font-weight:500}ol,ul{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:500}q:before,q:after{content:''}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}a:hover{text-decoration:underline}ins,a{text-decoration:none}",
b = document.createElement("style");
if (document.getElementsByTagName("head")[0].appendChild(b), b.styleSheet) b.styleSheet.disabled || (b.styleSheet.cssText =
a);
else try {
b.innerHTML = a
} catch (c) {
b.innerText = a
}
}();
! function (a, b) {
function c() {
var b = f.getBoundingClientRect().width;
b / i > 540 && (b = 540 * i);
var c = b / 10;
f.style.fontSize = c + "px", k.rem = a.rem = c
}
var d, e = a.document,
f = e.documentElement,
g = e.querySelector('meta[name="viewport"]'),
h = e.querySelector('meta[name="flexible"]'),
i = 0,
j = 0,
k = b.flexible || (b.flexible = {});
if (g) {
console.warn('將根據已有的meta標籤來設置縮放比例');
var l = g.getAttribute("content").match(/initial\-scale=([\d\.]+)/);
l && (j = parseFloat(l[1]), i = parseInt(1 / j))
} else if (h) {
var m = h.getAttribute("content");
if (m) {
var n = m.match(/initial\-dpr=([\d\.]+)/),
o = m.match(/maximum\-dpr=([\d\.]+)/);
n && (i = parseFloat(n[1]), j = parseFloat((1 / i).toFixed(2))), o && (i = parseFloat(o[1]), j = parseFloat((
1 / i).toFixed(2)))
}
}
if (!i && !j) {
var p = (a.navigator.appVersion.match(/android/gi), a.navigator.appVersion.match(/iphone/gi)),
q = a.devicePixelRatio;
i = p ? q >= 3 && (!i || i >= 3) ? 3 : q >= 2 && (!i || i >= 2) ? 2 : 1 : 1, j = 1 / i
}
if (f.setAttribute("data-dpr", i), !g) if (g = e.createElement("meta"), g.setAttribute("name", "viewport"), g.setAttribute(
"content", "initial-scale=" + j + ", maximum-scale=" + j + ", minimum-scale=" + j + ", user-scalable=no"),
f.firstElementChild) f.firstElementChild.appendChild(g);
else {
var r = e.createElement("div");
r.appendChild(g), e.write(r.innerHTML)
}
a.addEventListener("resize", function () {
clearTimeout(d), d = setTimeout(c, 300)
}, !1), a.addEventListener("pageshow", function (a) {
a.persisted && (clearTimeout(d), d = setTimeout(c, 300))
}, !1), "complete" === e.readyState ? e.body.style.fontSize = 12 * i + "px" : e.addEventListener("DOMContentLoaded", function () {
e.body.style.fontSize = 12 * i + "px"
}, !1), c(), k.dpr = a.dpr = i, k.refreshRem = c, k.rem2px = function (a) {
var b = parseFloat(a) * this.rem;
return "string" == typeof a && a.match(/rem$/) && (b += "px"), b
}, k.px2rem = function (a) {
var b = parseFloat(a) / this.rem;
return "string" == typeof a && a.match(/px$/) && (b += "rem"), b
}
}(window, window.lib || (window.lib = {}));
複製代碼
僅做我的學習記錄,看到的大神們以爲哪裏錯了還請指出,輕噴輕噴!