技術棧簡介:
媒體查詢:通過查詢當前屬於哪種設備, 讓網頁能夠在不同的設備下正常的預覽。(再通俗點來說,就是通過媒體查詢,能夠自動判斷該瀏覽器所在屏幕的大小)
rem:CSS3新增的相對長度單位,是指相對於根元素html
的font-size
計算值的大小。簡單可理解爲屏幕寬度的百分比。
flexible.js:用來適配移動端的javascript框架。根據寬度的不同設置不同的字體大小,樣式間距都使用rem作爲單位,不同屏幕大小適配不同的樣式。
下載normalize.css:
這一步可跳過,Normalize.css是一種CSS reset的替代方案。它在默認的HTML元素樣式上提供了跨瀏覽器的高度一致性。也就是說,使用了這個樣式,在絕大部分瀏覽器上,都能呈現出良好的效果。
前往normalize.css官網下載:http://necolas.github.io/normalize.css/
如果是使用的腳手架,那麼可直接使用npm安裝:
npm install normalize.css
下載flexible.js:
前往flexible.js的github源碼庫上進行下載:https://github.com/amfe/lib-flexible
其中的index.js即是flexible.js
測試的html頁面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/index.css">
<script src="js/flexible.js"></script>
<title>適配測試</title>
</head>
<body>
<div class="header">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
<div class="banner"></div>
<div class="nav_sel">
<a href="#" class="sel" style="background-color: aqua;">1</a>
<a href="#" class="sel" style="background-color: rgb(0, 255, 157);">2</a>
<a href="#" class="sel" style="background-color: aqua;">3</a>
<a href="#" class="sel" style="background-color: rgb(0, 255, 157);">4</a>
<a href="#" class="sel" style="background-color: aqua;">5</a>
</div>
</body>
</html>
測試的樣式:
我這裏將頁面以750px爲設計稿,並通過媒體查詢設置寬度不超過750px。
@media screen and (min-width:750px){
html{
font-size: 75px !important;
}
}
body{
min-width: 320px;
max-width: 750px;
width: 10rem;
margin: 0 auto;
line-height: 1.5;
background: black;
}
.header{
position: relative;
width: 10rem;
height: 1.2rem;
top: 0;
left: 50%;
transform: translateX(-50%);
background: burlywood;
}
.left{
width: 2rem;
height:1.2rem;
background: blue;
float: left;
}
.center{
flex: 1;
width: 6rem;
height:1.2rem;
background: chartreuse;
float: left;
}
.right{
width: 2rem;
height:1.2rem;
background: blue;
float: left;
}
.banner{
width: 10rem;
height: 4rem;
background: pink;
}
.nav_sel{
width: 10rem;
height:4rem;
background: orange;
}
.sel{
width: 2rem;
height:2rem;
float: left;
text-align: center;
}
從我的css樣式可以看到,我把body節點下的width設置爲了10rem,這是爲什麼呢?原因是flexible.js將屏幕分成了10等份,而1等份就是等於1rem。所以10rem就是100%的屏幕寬度。