移动端1px像素解决方式,从1px像素问题剖析像素及viewport

更新日期: 2017-11-21阅读: 6.1k标签: px

在移动端web开发过程中,如果你对边框设置border:1px,会发现,边框在某些手机机型上面显示的1px比实际感觉会变粗,这也就是1像素问题。如下图是对桌面浏览器和移动端border设置1px的比较。那么是什么导致这种原因的呢?


一、1px像素产生原因

首先,我们先对物理像素,设备独立像素,设备像素比等概念进行了解。

1、设备像素(device pixels)

设备中能控制显示的最小单位。能单独显示颜色的最小单位或点,称作像素点或像点。


屏幕尺寸及其屏幕分辨率

屏幕尺寸是以英寸为单位(1英寸=2.54厘米),其中手机屏幕尺寸是计算的手机对角线的长度大小。

PPI(屏幕像素密度),与屏幕分辨率及其屏幕尺寸有关系,计算公式为 (dw:屏幕横向分辨率,dh:屏幕竖向分辨率,w:屏幕尺寸)。

屏幕尺寸和屏幕分辨率示意图如下图:

2、设备独立像素(dpi)

每英寸的像素,类似于密度,即每英寸的像素点数量。


3、css像素与设备像素比(dpr)

dpr=设备像素/设备独立像素。可以通过window.devicePixelRatio获取设备的dpr值。

css像素是独立于设备逻辑的,用于逻辑上衡量像素的单位。

一般来说,在桌面的浏览器中,设备像素比(dpr)等于1,一个css像素就是代表的一个物理像素。而在移动端,大多数机型都不是为1,其中iphone的dpr普遍是2和3,那么一个css像素不再是对应一个物理像素,而是2个和3个物理像素。即我们通常在css中设置的width:1px,对应的便是物理像素中的2px。

下面以小米为例,看一下android的dpr:


4、viewport

在手机上viewport一般有:

layout viewport

手机一般为了容纳为桌面浏览器设计的网站,默认布局viewport宽度远大于屏幕的宽度,为了让用户看到网站全貌,缩小网站。例如,apple一般将viewport宽度定为980px。

visual viewport

屏幕的可视区域,即物理像素尺寸

ideal viewport

最适合移动设备的viewport,ideal viewport的宽度等于移动设备的屏幕宽度

为了让viewport能够等于ideal viewport,一般我们会添加meta标签

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<!--width=device-width:宽度为设备宽度-->
<!--initial-scale:缩放比为1-->
<!--user-scalable=no:是否允许用户自定义缩放-->


二、1px像素解决方式

我们知道了px是相对单位(关于px是相对单位还是绝对单位,很多地方说法不一,先保留观点,等查证后再更新),我们在设置border:1px的时候,对于不同的手机机型显示的宽度不一致(因dpr值不同)。

解决边框变粗的7种办法

1、0.5px边框

在2014年的 WWDC,“设计响应的Web体验” 一讲中,Ted O’Connor 讲到关于“retina
hairlines”(retina 极细的线):在retina屏上仅仅显示1物理像素的边框,开发者应该如何处理呢。
他们曾介绍到 iOS 8 和 OS X Yosemite 即将支持 0.5px 的边框:


0.5px边框

额的神呐!so easy! 果真如此吗?
这样还不够(WWDC幻灯片通常是“唬人”的),但是相差不多。
问题是 retina 屏的浏览器可能不认识0.5px的边框,将会把它解释成0px,没有边框。包括 iOS 7 和之前版本,OS X Mavericks 及以前版本,还有 Android 设备。

解决方案:
解决方案是通过 JavaScript 检测浏览器能否处理0.5px的边框,如果可以,给html标签元素添加个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);
}
// 脚本应该放在内,如果在里面运行,需要包装 $(document).ready(function() {})

然后,极细的边框样式就容易了:

div {
  border: 1px solid #bbb;
}
.hairlines div {
  border-width: 0.5px;
}

优点:

  • 简单,不需要过多代码

缺点:

  • 无法兼容安卓设备、 iOS 8 以下设备。
2、使用border-image实现

准备一张符合你要求的border-image:


底部边框

样式设置:

.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设置在边框的底部,所以使用的图片是2px高,上部的1px颜色为透明,下部的1px使用视觉规定的border的颜色。如果边框底部和顶部同时需要border,可以使用下面的border-image:

上下边框
上下边框

样式设置:

.border-image-1px {
  border-width: 1px 0;
  -webkit-border-image: url(linenew.png) 2 0 stretch;
  border-image: url(linenew.png) 2 0 stretch;
}

到目前为止,我们已经能在iphone上展现1px border的效果了。但是我们发现这样的方法在非视网膜屏上会出现border显示不出来的现象,于是使用Media Query做了一些兼容,样式设置如下:

.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;
  }
}

优点:

  • 可以设置单条,多条边框
  • 没有性能瓶颈的问题

缺点:

  • 修改颜色麻烦, 需要替换图片
  • 圆角需要特殊处理,并且边缘会模糊
3、使用background-image实现

background-image 跟border-image的方法一样,你要先准备一张符合你要求的图片。然后将边框模拟在背景上。
样式设置:

.background-image-1px {
  background: url(../img/line.png) repeat-x left bottom;
  -webkit-background-size: 100% 1px;
  background-size: 100% 1px;
}

优点:

  • 可以设置单条,多条边框
  • 没有性能瓶颈的问题

缺点:

  • 修改颜色麻烦, 需要替换图片
  • 圆角需要特殊处理,并且边缘会模糊
4、多背景渐变实现

与background-image方案类似,只是将图片替换为css3渐变。设置1px的渐变背景,50%有颜色,50%透明。
样式设置:

.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
}

优点:

  • 可以实现单条、多条边框
  • 边框的颜色随意设置

缺点:

  • 代码量不少
  • 圆角没法实现
  • 多背景图片有兼容性问题
5、使用box-shadow模拟边框

利用css 对阴影处理的方式实现0.5px的效果
样式设置:

.box-shadow-1px {
  box-shadow: inset 0px -1px 1px -1px #c8c7cc;
}

优点:

  • 代码量少
  • 可以满足所有场景

缺点:

  • 边框有阴影,颜色变浅
6、viewport + rem 实现

同时通过设置对应viewport的rem基准值,这种方式就可以像以前一样轻松愉快的写1px了。
在devicePixelRatio = 2 时,输出viewport:

<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no">

在devicePixelRatio = 3 时,输出viewport:

<meta name="viewport" content="initial-scale=0.3333333333333333, maximum-scale=0.3333333333333333, minimum-scale=0.3333333333333333, user-scalable=no">

这种兼容方案相对比较完美,适合新的项目,老的项目修改成本过大。
对于这种方案,可以看看《使用Flexible实现手淘H5页面的终端适配》
优点:

  • 所有场景都能满足
  • 一套代码,可以兼容基本所有布局

缺点:

  • 老项目修改代价过大,只适用于新项目
7、伪类 + transform 实现

对于老项目,有没有什么办法能兼容1px的尴尬问题了,个人认为伪类+transform是比较完美的方法了。
原理是把原先元素的 border 去掉,然后利用 :before 或者 :after 重做 border ,并 transform 的 scale 缩小一半,原先的元素相对定位,新做的 border 绝对定位。
单条border样式设置:

.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样式设置:

.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;
}

最好在使用前也判断一下,结合 JS 代码,判断是否 Retina 屏:

if(window.devicePixelRatio && devicePixelRatio >= 2){
  document.querySelector('ul').className = 'scale-1px';
}

优点:

  • 所有场景都能满足
  • 支持圆角(伪类和本体类都需要加border-radius)

缺点:

  • 对于已经使用伪类的元素(例如clearfix),可能需要多层嵌套
来源整理:简书地址http://www.jianshu.com/p/7e63f5a32636,
知乎地址:https://zhuanlan.zhihu.com/p/30640770


链接: https://www.fly63.com/article/detial/145

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!