关于1px、0.5px网上很多文章都讨论过了,就不在这里展开了,个人用过好几种方案,貌似就这种兼容性等各方面都比较合适,废话不多说直接上代码,sass/scss可直接拷贝使用。


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
  0.5px细线 mixin
  $direction:border方向,'all'、'left'、'right'、'top'、'bottom'
  $color:border颜色,默认全局分隔线颜色
  $type:border实虚线,默认solid
  $border-z-index: 边框z-index,默认1
  $content-z-index: 内容z-index,默认2
*/
@mixin thin-border($direction: $direction, $color: var(--color-separator), $type: solid, $border-z-index: 1, $content-z-index: 2) {
  position: relative;

  & > * {
    z-index: $content-z-index;
  }

  &:after {
    z-index: $border-z-index;
    content: " ";
    position: absolute;
    @if $direction == all {
      border: 1px $type $color;
    } @else {
      border: 0;
      border-#{$direction}: 1px $type $color;
    }
  }

  @media (-webkit-min-device-pixel-ratio: 1.5), (min-device-pixel-ratio: 1.5) {
    &:after {
      top: 0;
      left: 0;
      bottom: -25%;
      right: -25%;
      -webkit-transform: scale(0.8);
      -webkit-transform-origin: 0 0;
      transform: scale(0.8);
      transform-origin: 0 0;
    }
  }

  @media (-webkit-min-device-pixel-ratio: 2), (min-device-pixel-ratio: 2) {
    &:after {
      left: -50%;
      bottom: -50%;
      top: -50%;
      right: -50%;
      -webkit-transform: scale(0.5);
      -webkit-transform-origin: 50% 50%;
      transform: scale(0.5);
      transform-origin: 50% 50%;
    }
  }

  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    &:after {
      top: 0;
      left: 0;
      bottom: -150%;
      right: -150%;
      -webkit-transform: scale(0.4);
      -webkit-transform-origin: 0 0;
      transform: scale(0.4);
      transform-origin: 0 0;
    }
  }
}

2019-09-24更新:

上述方法在iOS10上会出现元素无法触发点击事件的问题,网上也提到一些通过额外增加元素的方法来解决,但会破坏封装需要在业务层逐条修改,通过评估,目前对项目影响较大的还是iOS10的问题,最后决定通过媒体查询来判断当前平台,iOS直接使用小数点单位的宽度,到目前为止能够满足需求。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 非iOS系统采用伪元素的方法实现0.5px
@supports not (-webkit-overflow-scrolling: touch) {
  // 上述方案
}

// iOS系统直接使用小数点px
@supports (-webkit-overflow-scrolling: touch) {
  @if $direction == all {
    border: 0.5px $type $color;
  } @else {
    border: 0;
    border-#{$direction}: 0.5px $type $color;
  }

  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    @if $direction == all {
      border: 0.33px $type $color;
    } @else {
      border: 0;
      border-#{$direction}: 0.33px $type $color;
    }
  }
}