在之前的項(xiàng)目中猿规,UI告訴我說我們移動(dòng)項(xiàng)目中的邊框全部都變粗了衷快。原諒我的近視眼,為什么我看不出什么差距了姨俩,結(jié)果UI把他的設(shè)計(jì)稿跟我的屏幕截圖跟我看蘸拔,居然真的不一樣Jχ!!调窍!
沒有辦法宝冕,只有在后面的版本中去修改了,但是要改的話邓萨,需要知道是為什么地梨。所以查了很多資料,終于搞懂了這個(gè)問題缔恳,并且總結(jié)了幾種方法宝剖。
造成邊框變粗的原因
其實(shí)這個(gè)原因很簡(jiǎn)單,因?yàn)閏ss中的1px并不等于移動(dòng)設(shè)備的1px歉甚,這些由于不同的手機(jī)有不同的像素密度万细。在window對(duì)象中有一個(gè)devicePixelRatio屬性,他可以反應(yīng)css中的像素與設(shè)備的像素比铃芦。
devicePixelRatio的官方的定義為:設(shè)備物理像素和設(shè)備獨(dú)立像素的比例雅镊,也就是 devicePixelRatio = 物理像素 / 獨(dú)立像素。
解決邊框變粗的7種辦法
1刃滓、0.5px邊框
在2014年的 WWDC仁烹,“設(shè)計(jì)響應(yīng)的Web體驗(yàn)” 一講中,Ted O’Connor 講到關(guān)于“retina
hairlines”(retina 極細(xì)的線):在retina屏上僅僅顯示1物理像素的邊框咧虎,開發(fā)者應(yīng)該如何處理呢卓缰。
他們?cè)榻B到 iOS 8 和 OS X Yosemite 即將支持 0.5px 的邊框:
額的神吶!so easy! 果真如此嗎砰诵?
這樣還不夠(WWDC幻燈片通常是“唬人”的)征唬,但是相差不多。
問題是 retina 屏的瀏覽器可能不認(rèn)識(shí)0.5px的邊框茁彭,將會(huì)把它解釋成0px总寒,沒有邊框。包括 iOS 7 和之前版本理肺,OS X Mavericks 及以前版本摄闸,還有 Android 設(shè)備。
解決方案:
解決方案是通過 JavaScript 檢測(cè)瀏覽器能否處理0.5px的邊框妹萨,如果可以年枕,給html標(biāo)簽元素添加個(gè)class。
if (window.devicePixelRatio && devicePixelRatio >= 2) {
var testElem = document.createElement('div');
testElem.style.border = '.5px solid transparent';
document.body.appendChild(testElem);
}
if (testElem.offsetHeight == 1) {
document.querySelector('html').classList.add('hairlines');
}
document.body.removeChild(testElem);
}
// 腳本應(yīng)該放在內(nèi)乎完,如果在里面運(yùn)行熏兄,需要包裝 $(document).ready(function() {})
然后,極細(xì)的邊框樣式就容易了:
div {
border: 1px solid #bbb;
}
.hairlines div {
border-width: 0.5px;
}
優(yōu)點(diǎn):
- 簡(jiǎn)單,不需要過多代碼摩桶。
缺點(diǎn):
- 無(wú)法兼容安卓設(shè)備桥状、 iOS 8 以下設(shè)備。
2典格、使用border-image實(shí)現(xiàn)
準(zhǔn)備一張符合你要求的border-image:
樣式設(shè)置:
.border-bottom-1px {
border-width: 0 0 1px 0;
-webkit-border-image: url(linenew.png) 0 0 2 0 stretch;
border-image: url(linenew.png) 0 0 2 0 stretch;
}
上文是把border設(shè)置在邊框的底部岛宦,所以使用的圖片是2px高,上部的1px顏色為透明耍缴,下部的1px使用視覺規(guī)定的border的顏色砾肺。如果邊框底部和頂部同時(shí)需要border,可以使用下面的border-image:
樣式設(shè)置:
.border-image-1px {
border-width: 1px 0;
-webkit-border-image: url(linenew.png) 2 0 stretch;
border-image: url(linenew.png) 2 0 stretch;
}
到目前為止防嗡,我們已經(jīng)能在iphone上展現(xiàn)1px border的效果了变汪。但是我們發(fā)現(xiàn)這樣的方法在非視網(wǎng)膜屏上會(huì)出現(xiàn)border顯示不出來(lái)的現(xiàn)象,于是使用Media Query做了一些兼容蚁趁,樣式設(shè)置如下:
.border-image-1px {
border-bottom: 1px solid #666;
}
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
.border-image-1px {
border-bottom: none;
border-width: 0 0 1px 0;
-webkit-border-image: url(../img/linenew.png) 0 0 2 0 stretch;
border-image: url(../img/linenew.png) 0 0 2 0 stretch;
}
}
優(yōu)點(diǎn):
- 可以設(shè)置單條,多條邊框
- 沒有性能瓶頸的問題
缺點(diǎn):
- 修改顏色麻煩, 需要替換圖片
- 圓角需要特殊處理裙盾,并且邊緣會(huì)模糊
3、使用background-image實(shí)現(xiàn)
background-image 跟border-image的方法一樣他嫡,你要先準(zhǔn)備一張符合你要求的圖片番官。然后將邊框模擬在背景上。
樣式設(shè)置:
.background-image-1px {
background: url(../img/line.png) repeat-x left bottom;
-webkit-background-size: 100% 1px;
background-size: 100% 1px;
}
優(yōu)點(diǎn):
- 可以設(shè)置單條,多條邊框
- 沒有性能瓶頸的問題
缺點(diǎn):
- 修改顏色麻煩, 需要替換圖片
- 圓角需要特殊處理钢属,并且邊緣會(huì)模糊
4徘熔、多背景漸變實(shí)現(xiàn)
與background-image方案類似,只是將圖片替換為css3漸變淆党。設(shè)置1px的漸變背景酷师,50%有顏色,50%透明染乌。
樣式設(shè)置:
.background-gradient-1px {
background:
linear-gradient(#000, #000 100%, transparent 100%) left / 1px 100% no-repeat,
linear-gradient(#000, #000 100%, transparent 100%) right / 1px 100% no-repeat,
linear-gradient(#000,#000 100%, transparent 100%) top / 100% 1px no-repeat,
linear-gradient(#000,#000 100%, transparent 100%) bottom / 100% 1px no-repeat
}
/* 或者 */
.background-gradient-1px{
background:
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) left / 1px 100% no-repeat,
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) right / 1px 100% no-repeat,
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) top / 100% 1px no-repeat,
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) bottom / 100% 1px no-repeat
}
優(yōu)點(diǎn):
- 可以實(shí)現(xiàn)單條山孔、多條邊框
- 邊框的顏色隨意設(shè)置
缺點(diǎn):
- 代碼量不少
- 圓角沒法實(shí)現(xiàn)
- 多背景圖片有兼容性問題
5、使用box-shadow模擬邊框
利用css 對(duì)陰影處理的方式實(shí)現(xiàn)0.5px的效果
樣式設(shè)置:
.box-shadow-1px {
box-shadow: inset 0px -1px 1px -1px #c8c7cc;
}
優(yōu)點(diǎn):
- 代碼量少
- 可以滿足所有場(chǎng)景
缺點(diǎn):
- 邊框有陰影荷憋,顏色變淺
6台颠、viewport + rem 實(shí)現(xiàn)
同時(shí)通過設(shè)置對(duì)應(yīng)viewport的rem基準(zhǔn)值,這種方式就可以像以前一樣輕松愉快的寫1px了勒庄。
在devicePixelRatio = 2 時(shí)串前,輸出viewport:
<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no">
在devicePixelRatio = 3 時(shí),輸出viewport:
<meta name="viewport" content="initial-scale=0.3333333333333333, maximum-scale=0.3333333333333333, minimum-scale=0.3333333333333333, user-scalable=no">
這種兼容方案相對(duì)比較完美锅铅,適合新的項(xiàng)目酪呻,老的項(xiàng)目修改成本過大减宣。
對(duì)于這種方案盐须,可以看看《使用Flexible實(shí)現(xiàn)手淘H5頁(yè)面的終端適配》
優(yōu)點(diǎn):
- 所有場(chǎng)景都能滿足
- 一套代碼,可以兼容基本所有布局
缺點(diǎn):
- 老項(xiàng)目修改代價(jià)過大漆腌,只適用于新項(xiàng)目
7贼邓、偽類 + transform 實(shí)現(xiàn)
對(duì)于老項(xiàng)目阶冈,有沒有什么辦法能兼容1px的尷尬問題了,個(gè)人認(rèn)為偽類+transform是比較完美的方法了塑径。
原理是把原先元素的 border 去掉女坑,然后利用 :before 或者 :after 重做 border ,并 transform 的 scale 縮小一半统舀,原先的元素相對(duì)定位匆骗,新做的 border 絕對(duì)定位。
單條border樣式設(shè)置:
.scale-1px{
position: relative;
border:none;
}
.scale-1px:after{
content: '';
position: absolute;
bottom: 0;
background: #000;
width: 100%;
height: 1px;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
}
四條boder樣式設(shè)置:
.scale-1px{
position: relative;
margin-bottom: 20px;
border:none;
}
.scale-1px:after{
content: '';
position: absolute;
top: 0;
left: 0;
border: 1px solid #000;
-webkit-box-sizing: border-box;
box-sizing: border-box;
width: 200%;
height: 200%;
-webkit-transform: scale(0.5);
transform: scale(0.5);
-webkit-transform-origin: left top;
transform-origin: left top;
}
最好在使用前也判斷一下誉简,結(jié)合 JS 代碼碉就,判斷是否 Retina 屏:
if(window.devicePixelRatio && devicePixelRatio >= 2){
document.querySelector('ul').className = 'scale-1px';
}
優(yōu)點(diǎn):
- 所有場(chǎng)景都能滿足
- 支持圓角(偽類和本體類都需要加border-radius)
缺點(diǎn):
- 對(duì)于已經(jīng)使用偽類的元素(例如clearfix),可能需要多層嵌套
參考:
- 《1px on retina》
- 《再談mobile web retina 下 1px 邊框解決方案》
- 《Retina屏的移動(dòng)設(shè)備如何實(shí)現(xiàn)真正1px的線闷串?》
- 《在retina屏中實(shí)現(xiàn)1px border效果》
順便貼一下本人的博客:yellowlemon的博客