mobile-web-border

解决移动端 1px 边框问题

主要思路是将 HTML元素border设置为0,再通过伪类::before 或 ::after 去放大、旋转实现。
我的习惯是将 scss 变量存放在 vars.scss; scss mixin 存放在 mixin.scss.

vars.scss

1
2
3
4
$border-colors: (
'main': #dddddd,
'active': #0088ff,
);

mixin.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// border mixin start

@mixin dpr2 {
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 2dppx) {
@content;
}
}
@mixin border-dpr2($bcolor, $border-radius) {
content: '';
position: absolute;
left: 0;
top: 0;
width: 200%;
height: 200%;
border: 1PX solid $bcolor;
transform-origin: 0 0;
transform: scale(0.5);
box-sizing: border-box;
pointer-events: none;
border-radius: $border-radius;
}
@mixin border($bcolor: map-get($border-colors , 'main'), $border-radius: 0) {
border: 1px solid $bcolor;
@include dpr2 {
position: relative;
border: none;
&::before {
@include border-dpr2($bcolor, $border-radius);
}
}
}
@mixin border-commom-dpr2($bcolor) {
content: '.';
position: absolute;
background-color: $bcolor;
display: block;
z-index: 1;
font-size: 0px;
}
@mixin border-bottom-dpr2($bcolor) {
top: auto;
right: auto;
bottom: 0;
left: 0;
width: 100%;
height: 1PX;
@include border-commom-dpr2($bcolor);
transform: scaleY(0.5);
// rotateY(-180deg) 抖动
// transform: rotateY(-180deg) scaleY(0.5);
}
@mixin border-top-dpr2($bcolor) {
top: 0;
right: auto;
bottom: auto;
left: 0;
width: 100%;
height: 1PX;
@include border-commom-dpr2($bcolor);
transform: scaleY(0.5);
}
@mixin border-left-dpr2($bcolor) {
top: 0;
right: auto;
bottom: auto;
left: 0;
width: 1PX;
height: 100%;
@include border-commom-dpr2($bcolor);
transform: rotateY(180deg) scaleX(0.5);
}
@mixin border-right-dpr2($bcolor) {
top: 0;
right: 0;
bottom: auto;
left: auto;
width: 1PX;
height: 100%;
@include border-commom-dpr2($bcolor);
transform: rotateY(180deg) scaleX(0.5);
}

@mixin border-bottom($bcolor: map-get($border-colors , 'main')) {
border-bottom: 1px solid $bcolor;
@include dpr2 {
position: relative;
border: none;
&::after {
@include border-bottom-dpr2($bcolor);
}
}
}
@mixin border-top($bcolor: map-get($border-colors , 'main')) {
border-top: 1px solid $bcolor;
@include dpr2 {
position: relative;
border: none;
&::after {
@include border-top-dpr2($bcolor);
}
}
}
@mixin border-left($bcolor: map-get($border-colors , 'main')) {
border-left: 1px solid $bcolor;
@include dpr2 {
position: relative;
border: none;
&::after {
@include border-left-dpr2($bcolor);
}
}
}
@mixin border-right($bcolor: map-get($border-colors , 'main')) {
border-right: 1px solid $bcolor;
@include dpr2 {
position: relative;
border: none;
&::after {
@include border-right-dpr2($bcolor);
}
}
}
// border mixin end


使用方法

如果使用 webpack 可以将 var.scss 和 mixin.scss 全局注入,方便使用。
或者在需要使用的 .scss 文件,import进入使用。

设置元素的单个边框,以上边框为例:

1
2
3
4
5
6
7
8
// default color
.border-top {
@include border-top;
}
// custom color
.border-top-active {
@include border-top($active);
}

设置元素所有边框

1
2
3
4
5
6
7
8
// default color
.border {
@include border;
}
// custom color
.border-active {
@include border($active);
}

Demo

活中充满了未知,充满了不确定,我们才会有继续生活下去的兴趣和愿望。 ——王小波