因爲分辨率 DPI 的差別,高清手機屏上的 1px 其實是由 2×2 個像素點來渲染,有的屏幕甚至用到了 3×3 個像素點javascript
因此 border: 1px 在移動端會渲染爲 2px 的邊框java
與設計圖產生差別,而且沒有那麼美觀。web
兩種解決方法:spa
1、transform:scalescala
使用僞類 :after 或者 :before 建立 1px 的邊框,而後經過 media 適配不一樣的設備像素比,而後調整縮放比例,從而實現一像素邊框設計
首先用僞類建立邊框code
.border-bottom{ position: relative; } .border-bottom::after { content: " "; position: absolute; left: 0; bottom: 0; width: 100%; height: 1px; background-color: #e4e4e4; }
而後經過媒體查詢來適配orm
/* 1.5倍屏 */ @media only screen and (-webkit-min-device-pixel-ratio: 1.5) { .border-bottom::after { -webkit-transform: scaleY(0.7); transform: scaleY(0.7); } } /* 2倍屏 */ @media only screen and (-webkit-min-device-pixel-ratio: 2.0) { .border-bottom::after { -webkit-transform: scaleY(0.5); transform: scaleY(0.5); } } /* 3倍屏 */ @media only screen and (-webkit-min-device-pixel-ratio: 3.0) { .border-bottom::after { -webkit-transform: scaleY(0.33); transform: scaleY(0.33); } }
這種辦法的邊框並非真正的 border,而是高度或者寬度爲 1px 的塊狀模型,因此這種辦法不能作出圓角,通常都用來畫分割線。blog
2、viewportip
網頁的內容都渲染在 viewport 上,因此設備像素比的差別,直接影響的也是 viewport 的大小
經過 js 獲取到設備像素比,而後動態添加 <meta> 標籤
<script type="text/javascript"> (function() { var scale = 1.0; if (window.devicePixelRatio === 2) { scale *= 0.5; } if (window.devicePixelRatio === 3) { scale *= 0.333333; } var text = '<meta name="viewport" content="initial-scale=' + scale + ', maximum-scale=' + scale +', minimum-scale=' + scale + ', width=device-width, user-scalable=no" />'; document.write(text); })(); </script>