最近研究移動端布局,rem 布局,總體思路是把頁面放大到 DPR*device-width 然后縮放 1/DPR 來解決問題,但是這樣做發(fā)現(xiàn)一個問題。比如:
我照著 iPhone6 375*667 開發(fā)一個導航條,在蘋果產(chǎn)品上都沒有問題,
但是換到 Pixel 2 上就有問題,如圖:
原因很明顯,小數(shù)誤差舍入問題因為5個導航條最終寬度 1078.85,而屏幕寬度 1078 放不下,所以換行了。
已有的方案:
margin-right:auto
overflow:hidden
當然,我知道,可以通過 flex
解決這個問題, vw
或許可以緩解這個問題,但是我沒采用,因為在使用 rem
布局的年代,vw
和 flex
的兼容性應該是不可以接受的,如果 vw
和 flex
的兼容性可以接受的話,也沒有必要使用 rem
布局了,不是么?
所以我想問,當時你們有什么更好的解決方案么?
代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
document.addEventListener(
'DOMContentLoaded',
() => {
let dpr = window.devicePixelRatio
let html = document.documentElement
let body = document.body
let deviceWidth = window.innerWidth || html.clientWidth
html.style.fontSize = (deviceWidth * dpr) / 10 + 'px'
html.setAttribute('data-dpr', dpr)
body.style.fontSize = 'initial'
body.style.fontSize = parseInt(getComputedStyle(body).fontSize) * dpr + 'px'
let metaViewport = document.querySelector('meta[name=viewport]')
metaViewport.setAttribute(
'content',
`width=${dpr * deviceWidth},initial-scale=${1 / dpr}`
)
},
{ capture: true },
true
)
</script>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet/less" href="./less.less">
<script src="http://cdnjs.cloudflare.com/ajax/libs/less.js/3.0.2/less.min.js"></script>
</head>
<body>
<nav class="nav">
<ul class="nav__list">
<li class="nav__item">
<a href="#" class="nav__link">nav1</a>
</li>
<li class="nav__item">
<a href="#" class="nav__link">nav2</a>
</li>
<li class="nav__item">
<a href="#" class="nav__link">nav3</a>
</li>
<li class="nav__item">
<a href="#" class="nav__link">nav4</a>
</li>
<li class="nav__item">
<a href="#" class="nav__link">nav5</a>
</li>
</ul>
</nav>
</body>
/* reset.css */
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
li {
list-style: none;
}
a {
text-decoration: none;
color: inherit;
}
.px2px(@name, @px) {
@{name}: round(@px) * 1px;
[data-dpr='2'] & {
@{name}: round(@px*2) * 1px;
}
// for mx3
[data-dpr='2.5'] & {
@{name}: round(@px * 2.5) * 1px;
}
//for Pixel2
[data-dpr='2.625'] & {
@{name}: round(@px * 2.625) * 1px;
}
// for XiaoMi note
[data-dpr='2.75'] & {
@{name}: round(@px * 2.75) * 1px;
}
[data-dpr='3'] & {
@{name}: round(@px * 3) * 1px;
}
//for Pixel2 XL
[data-dpr='3.5'] & {
@{name}: round(@px * 3.5) * 1px;
}
// for Samsung note4
[data-dpr='4'] & {
@{name}: @px * 4px;
}
}
/* base.css */
.nav {
//suggest use em not px otherwise have to set different font-size for different DPR
.px2px(font-size, 16px);
overflow: hidden;
width: 100%;
}
.nav__list {
// width: 110%;
width: 100%;
height: 100%;
// for inline-block
// white-space: nowrap;
overflow: hidden;
background-color: pink;
letter-spacing: -0.5em;
}
.nav__item {
letter-spacing: normal;
float: left;
// display: inline-block;
color: white;
background-color: yellowgreen;
width: 65/375 * 10rem;
height: 40/375 * 10rem;
margin: 5/375 * 10rem;
line-height: 40/375 * 10rem;
}
.nav__link {
display: block;
width: 100%;
height: 100%;
text-align: center;
}
如果是導航條的話,我用 inline-block + 百分比寬度來做
<div class="outer">
<div style="background-color: red"></div>
<div style="background-color: orange"></div>
<div style="background-color: yellow"></div>
<div style="background-color: green"></div>
<div style="background-color: blue"></div>
</div>
<style>
.outer {
/* 去掉inline-block中間的間隙 */
font-size: 0;
}
.outer div {
display: inline-block;
width: 20%;
height: 10px;
}
</style>
或者是 float
<div class="outer">
<div style="background-color: red"></div>
<div style="background-color: orange"></div>
<div style="background-color: yellow"></div>
<div style="background-color: green"></div>
<div style="background-color: blue"></div>
</div>
<style>
.outer div {
float: left;
width: 20%;
height: 10px;
}
.outer:after {
/* 清除浮動 */
content: '';
display: table;
clear: both;
}
</style>
效果:
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學院和江蘇省首批服務(wù)外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網(wǎng)
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務(wù)負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。