UNPKG

16 kBSCSSView Raw
1////
2/// @group utils
3////
4
5/// 清除浮动
6///
7/// @example scss
8/// .wrapper {
9/// @include clearfix;
10/// }
11///
12@mixin clearfix {
13 &:after {
14 visibility: hidden;
15 display: block;
16 height: 0;
17 font-size: 0;
18 content: '\0020';
19 clear: both;
20 }
21}
22
23
24
25/// 单行截取文字,添加『...』结尾
26///
27/// @param {Number | String} $width [100%] - 最大宽度,CSS长度,包括calc方法定义的表达式
28///
29/// @example scss - 使用
30/// .element {
31/// @include ellipsis;
32/// }
33///
34/// @example css - 输出
35/// .element {
36/// display: inline-block;
37/// max-width: 100%;
38/// overflow: hidden;
39/// text-overflow: ellipsis;
40/// white-space: nowrap;
41/// word-wrap: normal;
42/// }
43///
44@mixin ellipsis($width: 100%) {
45 display: inline-block;
46 max-width: $width;
47 overflow: hidden;
48 text-overflow: ellipsis;
49 white-space: nowrap;
50 word-wrap: normal;
51}
52
53/// 多行截取文字,添加『...』结尾
54///
55/// @param {Number | String} $line-height [1.2em] - 行高
56/// @param {Number} $line-count [2] - 行数, 超出行数开始截取
57/// @param {Color} $bg-color [#fff] - 文本背景色
58///
59/// @example scss - 使用
60/// .element {
61/// @include multi-line-ellipsis(1.2em, 3, #fff);
62/// }
63///
64/// @example css - 输出
65/// .element {
66/// overflow: hidden;
67/// position: relative;
68/// line-height: 1.2em;
69/// max-height: 3.6em;
70/// text-align: justify;
71/// padding-right: 0em;
72/// }
73///
74/// .element:before {
75/// content: '...';
76/// position: absolute;
77/// right: 0;
78/// bottom: 0;
79/// background: #fff;
80/// }
81///
82/// .element:after {
83/// content: '';
84/// position: absolute;
85/// right: 0;
86/// width: 1em;
87/// height: 1em;
88/// margin-top: 0.2em;
89/// background: #fff;
90/// }
91///
92@mixin multi-line-ellipsis(
93 $line-height: 1.2em,
94 $line-count: 2,
95 $bg-color: #fff
96) {
97 overflow: hidden;
98 position: relative;
99 line-height: $line-height;
100 max-height: $line-height * $line-count;
101 text-align: justify;
102 padding-right: 0;
103
104 &:before {
105 content: '...';
106 position: absolute;
107 right: 0;
108 bottom: 0;
109 background: $bg-color;
110 }
111
112 &:after {
113 content: '';
114 position: absolute;
115 right: 0;
116 width: 1em;
117 height: 1em;
118 margin-top: .2em;
119 background: $bg-color;
120 }
121}
122
123/// 隐藏文字
124///
125/// 『text-indent: -9999px』Hack 的替代版本
126///
127/// @link http://nicolasgallagher.com/another-css-image-replacement-technique
128///
129/// @example scss
130/// .element {
131/// @include hide-text;
132/// background: url(logo.png);
133/// }
134///
135@mixin hide-text {
136 font: 0/0 a;
137 text-shadow: none;
138 color: transparent;
139}
140
141/// 将元素垂直(水平)居中 (transform 版本)
142///
143/// @param {String} $inner-selector ['.inner'] - 直接子选择器名称
144/// @param {Bool} $horizontal [true] - 是否水平居中
145///
146/// @example scss - 使用
147/// .element {
148/// @include center-tl;
149/// }
150///
151/// @example css - 输出
152/// .element {
153/// position: relative;
154/// }
155///
156/// .element > .inner {
157/// position: absolute;
158/// top: 50%;
159/// left: 50%;
160/// transform: translate(-50%, -50%);
161/// }
162///
163@mixin center-tl(
164 $inner-selector: '.inner',
165 $horizontal: true
166) {
167
168 position: relative;
169
170 & > #{$inner-selector} {
171 position: absolute;
172 top: 50%;
173 $translate-val: translateY(-50%);
174
175 @if $horizontal {
176 left: 50%;
177 $translate-val: translate(-50%, -50%);
178 }
179
180 transform: $translate-val;
181 }
182}
183
184/// 将元素垂直(水平)居中 (table 版本)
185///
186/// @param {String} $inner-selector ['.inner'] - 直接子选择器名称
187/// @param {Bool} $horizontal [true] - 是否水平居中
188///
189/// @example scss - 使用
190/// .element {
191/// @include center-td;
192/// }
193///
194/// @example css - 输出
195/// .element {
196/// text-align: center;
197/// display: table;
198/// }
199///
200/// .element > .inner {
201/// display: table-cell;
202/// vertical-align: middle;
203/// }
204///
205@mixin center-td(
206 $inner-selector: '.inner',
207 $horizontal: true
208) {
209
210 @if $horizontal {
211 text-align: center;
212 }
213
214 display: table;
215
216 & > #{$inner-selector} {
217 display: table-cell;
218 vertical-align: middle;
219 }
220}
221
222/// 快捷设置元素width,height的方法
223///
224/// @param {String | List} $size - CSS尺寸长度
225///
226/// @example scss - 使用
227/// .element {
228/// @include size(2em 4em);
229/// }
230///
231/// @example css - 输出
232/// .element {
233/// width: 2em;
234/// height: 4em;
235/// }
236///
237@mixin size($size) {
238
239 @if length($size) == 1 {
240 width: $size;
241 height: $size;
242 } @else if length($size) == 2 {
243 width: nth($size, 1);
244 height: nth($size, 2);
245 }
246}
247
248// 快捷设置元素postion的方法 (传入null参数可略过一个方位)
249//
250// @param {String} $position [relative]
251// CSS position属性值
252//
253// @param {List} $coordinates [null null null null]
254// 上、右、下、左 四个边值,可以传入1 ~ 4个值
255//
256// @example scss - 使用
257// .element {
258// @include position(absolute, 0 null null 10px);
259// }
260//
261// @example css - 输出
262// .element {
263// position: absolute;
264// left: 10px;
265// top: 0;
266// }
267//
268// @require {function} is-length
269// @require {function} unpack
270@mixin position($position: relative, $coordinates: null null null null) {
271
272 // 如果参数是数组
273 @if type-of($position) == list {
274 $coordinates: $position;
275 $position: relative;
276 }
277
278 $coordinates: unpack($coordinates);
279
280 $offsets: (
281 top: nth($coordinates, 1),
282 right: nth($coordinates, 2),
283 bottom: nth($coordinates, 3),
284 left: nth($coordinates, 4)
285 );
286
287 position: $position;
288
289 @each $offset, $value in $offsets {
290
291 // 如果是合法长度
292 @if is-length($value) {
293 #{$offset}: $value;
294 }
295 }
296}
297
298// 三角型生成器(8种形态)
299//
300// @param {Number | List} $size
301// 三角形尺寸
302// 传入一个参数,生成等宽高三角形;传入数组且长度为2,第一个设置宽度,第二个设置高度
303//
304// @param {String | List} $color
305// 传入一个参数,设置三角形颜色;传入数组且长度为2,第一个设置三角形颜色,第二个设置背景色
306//
307// @param {String} $direction
308// 三角形朝向,可传参数:up | down | right | left | up-right | up-left | down-right | down-left | inset-up | inset-down | inset-left | inset-right
309//
310// @example scss - 使用
311// .element {
312// &:before {
313// content: " ";
314// @include triangle(100px 200px, blue, up);
315// }
316// }
317//
318// @example css - 输出
319// .example:before {
320// content: " ";
321// height: 0;
322// width: 0;
323// border-bottom: 200px solid blue;
324// border-left: 50px solid transparent;
325// border-right: 50px solid transparent;
326// }
327//
328@mixin triangle($size, $color, $direction) {
329 $width: nth($size, 1);
330 $height: nth($size, length($size));
331 $foreground-color: nth($color, 1);
332 $background-color: if(length($color) == 2, nth($color, 2), transparent);
333 height: 0;
334 width: 0;
335
336 @if ($direction == up) or ($direction == down) or ($direction == right) or ($direction == left) {
337 $width: $width / 2;
338 $height: if(length($size) > 1, $height, $height/2);
339
340 @if $direction == up {
341 border-bottom: $height solid $foreground-color;
342 border-left: $width solid $background-color;
343 border-right: $width solid $background-color;
344 } @else if $direction == right {
345 border-bottom: $width solid $background-color;
346 border-left: $height solid $foreground-color;
347 border-top: $width solid $background-color;
348 } @else if $direction == down {
349 border-left: $width solid $background-color;
350 border-right: $width solid $background-color;
351 border-top: $height solid $foreground-color;
352 } @else if $direction == left {
353 border-bottom: $width solid $background-color;
354 border-right: $height solid $foreground-color;
355 border-top: $width solid $background-color;
356 }
357 } @else if ($direction == up-right) or ($direction == up-left) {
358 border-top: $height solid $foreground-color;
359
360 @if $direction == up-right {
361 border-left: $width solid $background-color;
362 } @else if $direction == up-left {
363 border-right: $width solid $background-color;
364 }
365 } @else if ($direction == down-right) or ($direction == down-left) {
366 border-bottom: $height solid $foreground-color;
367
368 @if $direction == down-right {
369 border-left: $width solid $background-color;
370 } @else if $direction == down-left {
371 border-right: $width solid $background-color;
372 }
373 } @else if ($direction == inset-up) {
374 border-color: $background-color $background-color $foreground-color;
375 border-style: solid;
376 border-width: $height $width;
377 } @else if ($direction == inset-down) {
378 border-color: $foreground-color $background-color $background-color;
379 border-style: solid;
380 border-width: $height $width;
381 } @else if ($direction == inset-right) {
382 border-color: $background-color $background-color $background-color $foreground-color;
383 border-style: solid;
384 border-width: $width $height;
385 } @else if ($direction == inset-left) {
386 border-color: $background-color $foreground-color $background-color $background-color;
387 border-style: solid;
388 border-width: $width $height;
389 }
390}
391
392@mixin box-sizing {
393 box-sizing: border-box;
394 *,
395 *:before,
396 *:after {
397 box-sizing: border-box;
398 }
399}
400
401@mixin icon-size($size, $marginLeft: false, $marginRight: false, $transform: false) {
402 $size-static: get-compiling-value($size);
403 $icon-s-static: get-compiling-value($icon-s);
404 @if ($transform) {
405 transform: $transform;
406 }
407 @if ($marginLeft) {
408 margin-left: $marginLeft;
409 }
410 @if ($marginRight) {
411 margin-right: $marginRight;
412 }
413
414 &:before,
415 & .#{$css-prefix}icon-remote {
416 width: $size;
417 font-size: $size;
418 line-height: inherit;
419 }
420
421 // Chrome不支持小于12px的字体,故采用缩放的方式缩小字体
422 @if (type-of($size-static) == 'number') {
423 @if ($size-static < 12) {
424 @media all and (-webkit-min-device-pixel-ratio: 0) and (min-resolution: .001dpcm) {
425 @if ($transform) {
426 transform: scale(divide($size-static, $icon-s-static)) $transform;
427 } @else {
428 transform: scale(divide($size-static, $icon-s-static));
429 }
430 @if ($marginLeft) {
431 margin-left: calc(#{$marginLeft} - (#{$icon-s} - #{$size}) / 2);
432 } @else {
433 margin-left: calc(0px - (#{$icon-s} - #{$size}) / 2);
434 }
435 @if ($marginRight) {
436 margin-right: calc(#{$marginRight} - (#{$icon-s} - #{$size}) / 2);
437 } @else {
438 margin-right: calc(0px - (#{$icon-s} - #{$size}) / 2);
439 }
440
441 &:before {
442 width: $icon-s;
443 font-size: $icon-s;
444 }
445 }
446 }
447 }
448}
449
450@mixin icon-square-size($size, $marginTop: false, $marginRight: false, $marginBottom: false, $marginLeft: false, $transform: false) {
451 $size-static: get-compiling-value($size);
452 $icon-s-static: get-compiling-value($icon-s);
453 @if ($transform) {
454 transform: $transform;
455 }
456 @if ($marginTop) {
457 margin-top: $marginTop;
458 }
459 @if ($marginRight) {
460 margin-right: $marginRight;
461 }
462 @if ($marginBottom) {
463 margin-bottom: $marginBottom;
464 }
465 @if ($marginLeft) {
466 margin-left: $marginLeft;
467 }
468
469 width: $size;
470 height: $size;
471 line-height: 1em;
472
473 &:before {
474 width: $size;
475 height: $size;
476 font-size: $size;
477 line-height: 1em;
478 }
479
480 // Chrome不支持小于12px的字体,故采用缩放的方式缩小字体
481 @if (type-of($size-static) == 'number') {
482 @if ($size-static < 12) {
483 @media all and (-webkit-min-device-pixel-ratio: 0) and (min-resolution: .001dpcm) {
484 @if ($transform) {
485 transform: scale(divide($size-static, $icon-s-static)) $transform;
486 } @else {
487 transform: scale(divide($size-static, $icon-s-static));
488 }
489 @if ($marginTop) {
490 margin-top: calc(#{$marginTop} - (#{$icon-s} - #{$size}) / 2);
491 } @else {
492 margin-top: calc(0px - (#{$icon-s} - #{$size}) / 2);
493 }
494 @if ($marginRight) {
495 margin-right: calc(#{$marginRight} - (#{$icon-s} - #{$size}) / 2);
496 } @else {
497 margin-right: calc(0px - (#{$icon-s} - #{$size}) / 2);
498 }
499 @if ($marginBottom) {
500 margin-bottom: calc(#{$marginBottom} - (#{$icon-s} - #{$size}) / 2);
501 } @else {
502 margin-bottom: calc(0px - (#{$icon-s} - #{$size}) / 2);
503 }
504 @if ($marginLeft) {
505 margin-left: calc(#{$marginLeft} - (#{$icon-s} - #{$size}) / 2);
506 } @else {
507 margin-left: calc(0px - (#{$icon-s} - #{$size}) / 2);
508 }
509
510 width: $icon-s;
511 height: $icon-s;
512 line-height: $icon-s;
513
514 &:before {
515 width: $icon-s;
516 height: $icon-s;
517 font-size: $icon-s;
518 line-height: $icon-s;
519 }
520 }
521 }
522 }
523}
524
525@mixin font-face-handler($name, $url) {
526 @if ($name != Roboto) and ($name != "") {
527 @font-face {
528 font-family: "#{$name}";
529 src: url("#{$url}");
530 font-display: swap;
531 }
532 }
533}
534
535/// 设置 button 外观颜色
536///
537/// @param {String} $color 字体色
538/// @param {String} $color-hover 鼠标悬浮时的字体色
539/// @param {String} $color-active 鼠标按下时的字体色
540/// @param {String} $bg-color 背景色
541/// @param {String} $bg-color-hover 鼠标悬浮时的背景色
542/// @param {String} $bg-color-active 鼠标按下时的背景色
543/// @param {String} $border-color 边框色
544/// @param {String} $border-color-hover 鼠标悬浮时的边框色
545/// @param {String} $border-color-active 鼠标按下时的边框色
546@mixin button-color($color, $color-hover, $color-active, $bg-color: transparent, $bg-color-hover: transparent, $bg-color-active: transparent, $border-color: transparent, $border-color-hover: transparent, $border-color-active: transparent) {
547 background: $bg-color;
548 border-color: $border-color;
549
550 &,
551 &:link,
552 &:visited,
553 &.visited {
554 color: $color;
555 }
556
557 &:focus,
558 &:hover,
559 &.hover {
560 color: $color-hover;
561 background: $bg-color-hover;
562 border-color: $border-color-hover;
563 text-decoration: none;
564 }
565
566 &:active,
567 &.active {
568 color: $color-active;
569 background: $bg-color-active;
570 border-color: $border-color-active;
571 text-decoration: none;
572 }
573}
574
575/// 设置 button 大小
576///
577/// @param {Number} $padding 内边距
578/// @param {Number} $height 高度
579/// @param {Number} $font-size 字体大小
580/// @param {Number} $border-width 边框宽度
581@mixin button-size($padding, $height, $font-size, $border-width) {
582 padding: 0 $padding;
583 height: $height;
584 font-size: $font-size;
585 border-width: $border-width;
586}