最近研究移動端布局,rem 布局,總體思路是把頁面放大到 DPR*device-width 然后縮放 1/DPR 來解決問題,但是這樣做發(fā)現(xiàn)一個問題。比如:
我照著 iPhone6 375*667 開發(fā)一個導(dǎo)航條,在蘋果產(chǎn)品上都沒有問題,
但是換到 Pixel 2 上就有問題,如圖:
原因很明顯,小數(shù)誤差舍入問題因?yàn)?個導(dǎo)航條最終寬度 1078.85,而屏幕寬度 1078 放不下,所以換行了。
已有的方案:
margin-right:auto
overflow:hidden
當(dāng)然,我知道,可以通過 flex 解決這個問題, vw 或許可以緩解這個問題,但是我沒采用,因?yàn)樵谑褂?rem 布局的年代,vw 和 flex 的兼容性應(yīng)該是不可以接受的,如果 vw 和 flex 的兼容性可以接受的話,也沒有必要使用 rem 布局了,不是么?
所以我想問,當(dāng)時你們有什么更好的解決方案么?
代碼:
<!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;
}如果是導(dǎo)航條的話,我用 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年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學(xué)校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進(jìn)“中國制造2025”,實(shí)現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學(xué)優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。