分享我Scss常用的代码片段

包含Mixin圆角、背景图、渐变、阴影、清除浮动、边框、三角等

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//mixin.scss

@import "./utils.scss";

/*
* 作用:背景图片地址和大小
* 示例:@include back(url);
*/

@mixin back($url, $size: 100% 100%) {
background-image: url($url);
background-repeat: no-repeat;
background-size: $size;
}

/*
* 作用:盒模型
* 示例:@include box;
*/

@mixin box($sing: border-box) {
-webkit-box-sizing: $sing;
-moz-box-sizing: $sing;
box-sizing: $sing;
}

/*
* 作用:圆角
* 示例:@include radius;
*/

@mixin radius($radius: 10px) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
-ms-border-radius: $radius;
-o-border-radius: $radius;
border-radius: $radius;
}

/*
* 作用:定位全屏
* 示例:@include allcover;
*/

@mixin allcover {
position: absolute;
top: 0;
right: 0;
}

/*
* 作用:定位垂直水平居中
* 示例:@include center;
*/

@mixin center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

/*
* 作用:定位默认左上
* 示例:@include lt;
*/

@mixin lt($left: 0, $top: 0, $position: absolute) {
position: $position;
left: $left;
top: $top;
}

/*
* 作用:定位默认右上
* 示例:@include rt;
*/

@mixin rt($right: 0, $top: 0, $position: absolute) {
position: $position;
right: $right;
top: $top;
}

/*
* 作用:定位默认左下
* 示例:@include lb;
*/

@mixin lb($left: 0, $bottom: 0, $position: absolute) {
position: $position;
left: $left;
bottom: $bottom;
}

/*
* 作用:定位默认右下
* 示例:@include rb;
*/

@mixin rb($right: 0, $bottom: 0, $position: absolute) {
position: $position;
right: $right;
bottom: $bottom;
}

/*
* 作用:定位上下居中
* 示例:@include ct;
*/

@mixin ct {
position: absolute;
top: 50%;
transform: translateY(-50%);
}

/*
* 作用:定位左右居中
* 示例:@include cl;
*/

@mixin cl {
position: absolute;
left: 50%;
transform: translateX(-50%);
}

/*
* 作用:设置宽高 (默认宽高百分比)
* 示例:@include wh;
*/

@mixin wh($width: 100%, $height: 100%) {
width: $width;
height: $height;
}

/*
* 作用:设置字体大小、行高、颜色、对齐方式;
* 示例:@include font(20px,30px,#cccccc,center);
*/

@mixin font($size, $line-height, $color, $align: left) {
font-size: $size;
line-height: $line-height;
color: $color;
text-align: $align;
}

/*
* 作用:flex布局 及 子元素对齐方式
* 示例:@include flex;
*/

@mixin flex($type: space-between) {
display: flex;
justify-content: $type;
}

/*
* 作用:溢出显示点点点 (默认一行)
* 示例:
@include line;
@include line(3);
*/

@mixin line($num: 1) {
overflow: hidden;
text-overflow: ellipsis;
@if ($num == 1) {
white-space: nowrap;
} @else {
display: -webkit-box;
-webkit-line-clamp: $num;
-webkit-box-orient: vertical;
}
}

/*
* 作用:默认阴影
* 示例:@include shadow;
*/

@mixin shadow($radius: 10px, $shadow: 0 4px 20px rgba(0, 0, 0, 0.1)) {
box-shadow: $shadow;
@include radius($radius);
}

/*
* 作用:清除浮动
* 使用:@include clearfix;
*/

@mixin clearfix {
&:after,
&:before {
content: " ";
display: table;
}

&:after {
clear: both;
}
}

/*
* 作用:去除滚动条 (仅支持的是webkit内核的浏览器)
* 使用:@include no-scrollbar;
*/

@mixin no-scrollbar {
&::-webkit-scrollbar {
display: none !important;
width: 0 !important;
height: 0 !important;
-webkit-appearance: none;
opacity: 0 !important;
}
}

/*
* 一像素边框
* $direction: 边框方向,默认底边框
* $style: 线条样式,默认solid
* $color: 边框颜色
*
* 默认下边框 @include one-border;
*/
@mixin one-border($direction: bottom, $style: solid, $color: #e5e5e5) {
position: relative;
$border: 1px $style $color;
@if ($direction == bottom) {
&:after {
@include onepxTopBottom;
@include setDprBorder(tb);
border-top: $border;
bottom: 0;
}
} @else if ($direction == top) {
&:before {
@include onepxTopBottom;
@include setDprBorder(tb);
border-top: $border;
top: 0;
}
} @else if ($direction == left) {
&:before {
@include onepxLeftRight;
@include setDprBorder(lr);
border-left: $border;
left: 0;
}
} @else if ($direction == right) {
&:after {
@include onepxLeftRight;
@include setDprBorder(lr);
border-left: $border;
right: 0;
}
}
}

// 四边一像素边框
@mixin full-border($color: #e5e5e5, $radius: 0, $zIndex: -1) {
position: relative;
z-index: 1;
&:before {
content: "";
position: absolute;
z-index: $zIndex;
border: 1px solid $color;
width: 200%;
height: 200%;
border-radius: inherit;
transform: scale(0.5);
transform-origin: top left;
border-radius: $radius * 2;
left: 0;
top: 0;
}
}

/*
* 作用:px转rem
*
* 示例:两种使用方法
@include px2rem(height, 400px);
@include px2rem(width, 200px);
margin: px2rem(10px);
*/

@mixin px2rem($attr, $num, $base: 37.5) {
$list: (); //存储所有转换后的值

// 遍历所有值并转换为rem单位
@for $i from 1 through length($num) {
// 计算单个rem值
$value: strip-units(nth($num, $i)) / $base * 1rem;
// 添加到列表中
$list: append($list, $value);
}

// 设置属性值
#{$attr}: $list;
}

@function px2rem($num, $base: 37.5) {
@return strip-units($num) / $base * 1rem;
}

/*
* 作用:简单的渐变
*
* 示例:
@include background-gradient(rgba(255,146,156,1), rgba(216,0,0,1), vertical);
@include background-gradient(rgba(255,146,156,1), rgba(216,0,0,1), radius)
*/

@mixin background-gradient($start-color, $end-color, $orientation: vertical) {
background: $start-color;

@if $orientation == vertical {
background: linear-gradient(to bottom, $start-color, $end-color);
} @else if $orientation == horizontal {
background: linear-gradient(to right, $start-color, $end-color);
} @else {
background: radial-gradient(ellipse at center, $start-color, $end-color);
}
}

/*
* 作用:三角
* 示例:@include triangle(left, 10px);
*/

@mixin triangle($direction: down, $size: 5px, $color: #f96001) {
width: 0px;
height: 0px;
@if ($direction == left) {
border-top: $size solid transparent;
border-bottom: $size solid transparent;
border-right: $size solid $color;
} @else if ($direction == right) {
border-top: $size solid transparent;
border-bottom: $size solid transparent;
border-left: $size solid $color;
} @else if ($direction == down) {
border-left: $size solid transparent;
border-right: $size solid transparent;
border-top: $size solid $color;
} @else {
border-left: $size solid transparent;
border-right: $size solid transparent;
border-bottom: $size solid $color;
}
}
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
// utils.scss 是 mixin.scss 所需要用的辅助方法

@mixin onepxElement {
content: '';
position: absolute;
}

@mixin onepxTopBottom {
@include onepxElement;
left: 0;
right: 0;
}

@mixin onepxLeftRight {
@include onepxElement;
top: 0;
bottom: 0;
}

@mixin setDprBorder($direction: tb) {
@for $i from 1 through 4 {
@media screen and (-webkit-min-device-pixel-ratio: $i) {
@if ($direction == tb) {
transform: scaleY(1 / $i);
} @else if($direction == lr) {
transform: scaleX(1 / $i);
} @else if($direction == full) {
transform: scale(1 / $i);
}
}
}
}

@function strip-units($number) {
@return $number / ($number * 0 + 1);
}
附:

小兀666大佬的分享常用的CSS函数,助你写出更简洁的代码