# Less & Scss 中的 @mixin 用法

// 使用
// 先引入
@import '~@/assets/mixin.scss';

.popup-txt {
  width: 100%;
  height: 100px;
  font-size: 14px;
  box-sizing: border-box;
  padding-top: 24px;
  // 要使用的地方通过 @include 引入样式
  @include border-bottom-1px(#eeeeee);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 背景图片
@mixin bgImage($path) {
  background-image: url($path + '@2x.png');
  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    background-image: url($path + '@3x.png');
  }
}

// 居中布局
@mixin center() {
  justify-content: center;
  align-content: center;
}

// 不换行
@mixin no-wrap() {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

@mixin top-line($c: #c7c7c7) {
  content: ' ';
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 1px;
  border-top: 1px solid $c;
  color: $c;
  transform-origin: 0 0;
  transform: scaleY(0.5);
  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    transform: scaleY(0.333333333);
  }
}

@mixin bottom-line($c: #E6E6E6) {
  content: ' ';
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  height: 1px;
  border-bottom: 1px solid $c;
  color: $c;
  transform-origin: 0 100%;
  transform: scaleY(0.5);
  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    transform: scaleY(0.333333333);
  }
}

@mixin left-line($c: #c7c7c7) {
  content: ' ';
  position: absolute;
  left: 0;
  top: 0;
  width: 1px;
  bottom: 0;
  border-left: 1px solid $c;
  color: $c;
  transform-origin: 0 0;
  transform: scaleX(0.5);
  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    transform: scaleX(0.333333333);
  }
}

@mixin right-line($c: #c7c7c7) {
  content: ' ';
  position: absolute;
  right: 0;
  top: 0;
  width: 1px;
  bottom: 0;
  border-right: 1px solid $c;
  color: $c;
  transform-origin: 100% 0;
  transform: scaleX(0.5);
  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    transform: scaleX(0.333333333);
  }
}

@mixin line($c: #c7c7c7, $border-radius: 0) {
  content: ' ';
  position: absolute;
  left: 0;
  top: 0;
  width: 200%;
  height: 200%;
  border: 1px solid $c;
  color: $c;
  transform-origin: left top;
  @if ($border-radius != 0) {
    border-radius: $border-radius * 2;
  }
  transform: scale(0.5);
  @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3) {
    @if ($border-radius != 0) {
      border-radius: $border-radius * 3;
    }
    width: 300%;
    height: 300%;
    transform: scale(0.333333333);
  }
}

// 一像素上边框
@mixin border-top-1px($color: #c7c7c7) {
  position: relative;
  &:before {
    @include top-line();
  }
}

// 一像素下边框
@mixin border-bottom-1px($color: #c7c7c7) {
  position: relative;
  &:after {
    @include bottom-line();
  }
}

// 一像素上下边框
@mixin border-top-bottom-1px($color: #c7c7c7) {
  position: relative;
  &:before {
    @include top-line();
  }
  &:after {
    @include bottom-line();
  }
}

// 一像素左边框
@mixin border-left-1px($color: #c7c7c7) {
  position: relative;
  &:before {
    @include left-line();
  }
}

// 一像素右边框
@mixin border-right-1px($color: #c7c7c7) {
  position: relative;
  &:before {
    @include right-line();
  }
}

// 一像素边框
@mixin border-1px($color: #c7c7c7, $position: relative, $border-radius: 0) {
  position: $position;
  &:after {
    @include line($color, $border-radius);
  }
}
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159