最近完成了公司安排的移動(dòng)web觸屏開發(fā),期間涉及到在移動(dòng)設(shè)備上顯示線條,最開始采用PC常用的css board屬性來顯示1個(gè)像素的線條,但是發(fā)現(xiàn)在移動(dòng)設(shè)備上并不美觀,參考淘寶、京東的觸屏發(fā)現(xiàn)它們均是采用淺細(xì)的線條來顯示在移動(dòng)設(shè)備上。
以下紀(jì)錄了比較方便的4種繪制0.5像素線條方式
一、采用meta viewport的方式,這個(gè)也是淘寶觸屏采用的方式
常用的移動(dòng)html viewport的設(shè)置如下
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
具體意思就不多提,它就是讓頁面的高寬度即為設(shè)備的高寬像素,而為了方便繪制0.5像素的viewport的設(shè)置如下
<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no" />
這樣html的寬高就是設(shè)備的2倍,此時(shí)依然使用css board為1像素的話,肉眼看到頁面線條就相當(dāng)于transform:scale(0.5)的效果,即為0.5像素
但是這種方式涉及到頁面整體布局規(guī)劃以及圖片大小的制作,所以若采用這個(gè)方式還是事先確定為好
二、采用 border-image的方式
這個(gè)其實(shí)就比較簡單了,直接制作一個(gè)0.5像素的線條和其搭配使用的背景色的圖片即可
<!DOCTYPE html><head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title>boardTest</title> <style> p{ margin: 50px auto; padding: 5px 10px 5px 10px; color: red; text-align: center; width: 60px; } p:first-child{ border-bottom: 1px solid red; } p:last-child{ border-width: 0 0 1px 0; border-image: url("img/line_h.gif") 2 0 round; } </style><body> <div> <p>點(diǎn)擊1</p> <p>點(diǎn)擊2</p> </div></html>
三、采用background-image的方式
我這里采用的是漸變色linear-gradient的方式,代碼如下
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title>boardTest</title> <style> p{ margin: 50px auto; padding: 5px 10px 5px 10px; color: red; text-align: center; width: 60px; } p:first-child{ border-bottom: 1px solid red; } p:last-child{ background-image: -webkit-linear-gradient(bottom,red 50%,transparent 50%); background-image: linear-gradient(bottom,red 50%,transparent 50%); background-size: 100% 1px; background-repeat: no-repeat; background-position: bottom right; </style></head><body> <div> <p>點(diǎn)擊1</p> <p>點(diǎn)擊2</p> </div></body></html>
linear-gradient(bottom,red 50%,transparent 50%);的意思是從底部繪制一個(gè)漸變色,顏色為紅色,占比為50%,而總寬度已經(jīng)設(shè)置為100%而總高度為一個(gè)像素background-size: 100% 1px;
這樣顯示出來就是0.5像素的線條
四、采用transform: scale()的方式
就是將繪制出來的線條的高度進(jìn)行半倍的縮放,代碼如下
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title>boardTest</title> <style> p{ margin: 50px auto; padding: 5px 10px 5px 10px; color: red; text-align: center; width: 60px; } p:first-child{ border-bottom: 1px solid red; } p:last-child{ position: relative; } p:last-child:after { position: absolute; content: ''; width: 100%; left: 0; bottom: 0; height: 1px; background-color: red; -webkit-transform: scale(1,0.5); transform: scale(1,0.5); -webkit-transform-origin: center bottom; transform-origin: center bottom } </style></head><body> <div> <p>點(diǎn)擊1</p> <p>點(diǎn)擊2</p> </div></body></html>
以上就是小編為大家整理出來的Web前端繪制0.5像素的幾種方法,希望對大家的學(xué)習(xí)能夠起到幫助~
新聞熱點(diǎn)
疑難解答
圖片精選