1 | //************************************************************************// |
---|
2 | // Background property for adding multiple backgrounds using shorthand |
---|
3 | // notation. |
---|
4 | //************************************************************************// |
---|
5 | |
---|
6 | @mixin background( |
---|
7 | $background-1 , $background-2: false, |
---|
8 | $background-3: false, $background-4: false, |
---|
9 | $background-5: false, $background-6: false, |
---|
10 | $background-7: false, $background-8: false, |
---|
11 | $background-9: false, $background-10: false, |
---|
12 | $fallback: false |
---|
13 | ) { |
---|
14 | $backgrounds: compact($background-1, $background-2, |
---|
15 | $background-3, $background-4, |
---|
16 | $background-5, $background-6, |
---|
17 | $background-7, $background-8, |
---|
18 | $background-9, $background-10); |
---|
19 | |
---|
20 | $fallback-color: false; |
---|
21 | @if (type-of($fallback) == color) or ($fallback == "transparent") { |
---|
22 | $fallback-color: $fallback; |
---|
23 | } |
---|
24 | @else { |
---|
25 | $fallback-color: _extract-background-color($backgrounds); |
---|
26 | } |
---|
27 | |
---|
28 | @if $fallback-color { |
---|
29 | background-color: $fallback-color; |
---|
30 | } |
---|
31 | background: _background-add-prefix($backgrounds, webkit); |
---|
32 | background: _background-add-prefix($backgrounds); |
---|
33 | } |
---|
34 | |
---|
35 | @function _extract-background-color($backgrounds) { |
---|
36 | $final-bg-layer: nth($backgrounds, length($backgrounds)); |
---|
37 | @if type-of($final-bg-layer) == list { |
---|
38 | @for $i from 1 through length($final-bg-layer) { |
---|
39 | $value: nth($final-bg-layer, $i); |
---|
40 | @if type-of($value) == color { |
---|
41 | @return $value; |
---|
42 | } |
---|
43 | } |
---|
44 | } |
---|
45 | @return false; |
---|
46 | } |
---|
47 | |
---|
48 | @function _background-add-prefix($backgrounds, $vendor: false) { |
---|
49 | $backgrounds-prefixed: (); |
---|
50 | |
---|
51 | @for $i from 1 through length($backgrounds) { |
---|
52 | $shorthand: nth($backgrounds, $i); // Get member for current index |
---|
53 | $type: type-of($shorthand); // Get type of variable - List (gradient) or String (image) |
---|
54 | |
---|
55 | // If shorthand is a list (gradient) |
---|
56 | @if $type == list { |
---|
57 | $first-member: nth($shorthand, 1); // Get first member of shorthand |
---|
58 | |
---|
59 | // Linear Gradient |
---|
60 | @if index(linear radial, nth($first-member, 1)) { |
---|
61 | $gradient-type: nth($first-member, 1); // linear || radial |
---|
62 | $gradient-args: false; |
---|
63 | $gradient-positions: false; |
---|
64 | $shorthand-start: false; |
---|
65 | @if type-of($first-member) == list { // Linear gradient plus additional shorthand values - lg(red,orange)repeat,... |
---|
66 | $gradient-positions: nth($first-member, 2); |
---|
67 | $gradient-args: nth($first-member, 3); |
---|
68 | $shorthand-start: 2; |
---|
69 | } |
---|
70 | @else { // Linear gradient only - lg(red,orange),... |
---|
71 | $gradient-positions: nth($shorthand, 2); |
---|
72 | $gradient-args: nth($shorthand, 3); // Get gradient (red, blue) |
---|
73 | } |
---|
74 | |
---|
75 | $gradient-positions: _gradient-positions-parser($gradient-type, $gradient-positions); |
---|
76 | $gradient: _render-gradients($gradient-positions, $gradient-args, $gradient-type, $vendor); |
---|
77 | |
---|
78 | // Append any additional shorthand args to gradient |
---|
79 | @if $shorthand-start { |
---|
80 | @for $j from $shorthand-start through length($shorthand) { |
---|
81 | $gradient: join($gradient, nth($shorthand, $j), space); |
---|
82 | } |
---|
83 | } |
---|
84 | $backgrounds-prefixed: append($backgrounds-prefixed, $gradient, comma); |
---|
85 | } |
---|
86 | // Image with additional properties |
---|
87 | @else { |
---|
88 | $backgrounds-prefixed: append($backgrounds-prefixed, $shorthand, comma); |
---|
89 | } |
---|
90 | } |
---|
91 | // If shorthand is a simple string (color or image) |
---|
92 | @else if $type == string { |
---|
93 | $backgrounds-prefixed: join($backgrounds-prefixed, $shorthand, comma); |
---|
94 | } |
---|
95 | } |
---|
96 | @return $backgrounds-prefixed; |
---|
97 | } |
---|
98 | |
---|
99 | //Examples: |
---|
100 | //@include background(linear-gradient(top, orange, red)); |
---|
101 | //@include background(radial-gradient(circle at 40% 40%, orange, red)); |
---|
102 | //@include background(url("/images/a.png") no-repeat, linear-gradient(orange, red)); |
---|
103 | //@include background(url("image.png") center center, linear-gradient(orange, red), url("image.png")); |
---|