原码笔记

原码笔记

Web前端绘制0.5像素的几种方法

小诸哥 0

最近完成了公司安排的移动web触屏开发,期间涉及到在移动设备上显示线条,最开始采用PC常用的css board属性来显示1个像素的线条,但是发现在移动设备上并不美观,参考淘宝、京东的触屏发现它们均是采用浅细的线条来显示在移动设备上。

  以下纪录了比较方便的4种绘制0.5像素线条方式

一、采用meta viewport的方式,这个也是淘宝触屏采用的方式

常用的移动html viewport的设置如下

<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
具体意思就不多提,它就是让页面的高宽度即为设备的高宽像素,而为了方便绘制0.5像素的viewport的设置如下

<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no" />
这样html的宽高就是设备的2倍,此时依然使用css board为1像素的话,肉眼看到页面线条就相当于transform:scale(0.5)的效果,即为0.5像素

但是这种方式涉及到页面整体布局规划以及图片大小的制作,所以若采用这个方式还是事先确定为好

二、采用 border-image的方式

这个其实就比较简单了,直接制作一个0.5像素的线条和其搭配使用的背景色的图片即可

  1. <!DOCTYPE html>
  2. <head>
  3.      <meta charset="utf-8">
  4.      <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  5.      <title>boardTest</title>
  6.      <style>
  7.              p{
  8.                  margin: 50px auto;
  9.                  padding: 5px 10px 5px 10px;
  10.                  color: red;
  11.                  text-align: center;
  12.                  width: 60px;
  13.              }
  14.              p:first-child{
  15.                  border-bottom: 1px solid red;
  16.              }
  17.              p:last-child{
  18.                  border-width: 0 0 1px 0; border-image: url("img/line_h.gif") 2 0 round;
  19.              }
  20.      </style>
  21. <body>
  22.      <div>
  23.          <p>点击1</p>
  24.          <p>点击2</p>
  25.      </div>
  26. </html>
  27.  

三、采用background-image的方式

我这里采用的是渐变色linear-gradient的方式,代码如下

  1. <!DOCTYPE html>
  2. <html>
  3. <head&gt;
  4.      <meta charset="utf-8">
  5.      <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  6.      <title>boardTest</title>
  7.      <style>
  8.              p{
  9.              margin: 50px auto;
  10.                  padding: 5px 10px 5px 10px;
  11.                  color: red;
  12.                  text-align: center;
  13.                  width: 60px;
  14.              }
  15.              p:first-child{
  16.                  border-bottom: 1px solid red;
  17.              }
  18.              p:last-child{
  19.                  background-image: -webkit-linear-gradient(bottom,red 50%,transparent 50%);
  20.                  background-image: linear-gradient(bottom,red 50%,transparent 50%);
  21.                  background-size: 100% 1px;
  22.                  background-repeat: no-repeat;
  23.                  background-position: bottom right;
  24.      </style>
  25. </head>
  26. <body>
  27.      <div>
  28.          <p>点击1</p>
  29.          <p>点击2</p>
  30.      </div>
  31. </body>
  32. </html>
  33.  

linear-gradient(bottom,red 50%,transparent 50%);的意思是从底部绘制一个渐变色,颜色为红色,占比为50%,而总宽度已经设置为100%而总高度为一个像素background-size:  100% 1px;

这样显示出来就是0.5像素的线条

四、采用transform: scale()的方式

就是将绘制出来的线条的高度进行半倍的缩放,代码如下

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.      <meta charset="utf-8">
  5.      <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  6.      <title>boardTest</title>
  7.      <style>
  8.              p{
  9.                  margin: 50px auto;
  10.                  padding: 5px 10px 5px 10px;
  11.      color: red;
  12.                  text-align: center;
  13.                  width: 60px;
  14.              }
  15.              p:first-child{
  16.                  border-bottom: 1px solid red;
  17.              }
  18.              p:last-child{
  19.                  position: relative;
  20.              }
  21.              p:last-child:after {
  22.                  position: absolute;
  23.                  content: '';
  24.                  width: 100%;
  25.                  left: 0;
  26.                  bottom: 0;
  27.                  height: 1px;
  28.                  background-color: red;
  29.                  -webkit-transform: scale(1,0.5);
  30.                  transform: scale(1,0.5);
  31.                  -webkit-transform-origin: center bottom;
  32.                  transform-origin: center bottom
  33.              }
  34.         
  35.      </style>
  36. </head>
  37. <body>
  38.      <div>
  39.          <p>点击1</p>
  40.          <p>点击2</p>
  41.      </div>
  42. </body>
  43. </html>

 以上就是小编为大家整理出来的Web前端绘制0.5像素的几种方法,希望对大家的学习能够起到帮助~

标签: 前端绘制