移動web之一像素問題

來源:互聯網
上載者:User

標籤:tran   螢幕   gpo   viewport   back   before   ati   col   裝置   

一、為什麼會有一像素問題

弄明白這個問題,首先要知道DPR了。

  DPR(device pixel ratio)pixel等於picture element。裝置像素比,是預設縮放100%的情況下,即DPR=裝置像素個數/理想視口像素個數(device-width)。

  1.裝置像素:裝置的物理像素,任何裝置的物理像素都是固定不變的。

  2.布局視口:移動端css布局所依據的視口,即css布局是根據布局視口來計算的。理想視口即為理想的布局視口。

拿iPhone6為例,當我們寫一像素線border-top:1px solid red;時,螢幕顯示的1px的紅線嗎?顯然不是。布局視口是移動端我們css布局的依據視口,在我們設定<meta name="viewport" content="width:device-width">時就將布局視口設定為理想視口了。iPhone6的DPR=2,因此在螢幕上顯示的其實是2px的紅線。有人說,那我們寫css布局時寫border-top:0.5 solid red;不就完了嗎。想法很好,但是電腦會將0.5計算成1。
二、transform:scale

這裡的解決辦法是對這條線進行縮放,使用虛擬元素::before或者::after來實現一像素邊框,再利用@media適配不同的裝置像素比,來確定縮放比例。

.border-top {    position: relative;}.border-top::before {    position: absolute;    top: 0;    left: 0;    content: " ";    width: 100%;    height: 1px;    background-color: #e4e4e4;    -webkit-transform-origin: left bottom;    transform-origin: left bottom;}/* 2倍屏 */@media  (-webkit-min-device-pixel-ratio: 2.0),(min-device-pixel-ratio: 2.0){    .border-bottom::before {        -webkit-transform: scaleY(0.5);        transform: scaleY(0.5);    }}/* 3倍屏 */@media (-webkit-min-device-pixel-ratio: 3.0),(min-device-pixel-ratio: 3.0) {    .border-bottom::before {        -webkit-transform: scaleY(0.33);        transform: scaleY(0.33);    }}

 

  

移動web之一像素問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.