对于那些希望从具有自定义属性的渐变创建网格的人来说,这就是我使用 sass/scss 实现此目的的方式:
@mixin buildGridGradient($c1: #444, $c2: #111, $c3: #222, $gutter: $gutterWidth, $nc: $numberOfColumns, $c: $columnWidth) {
$gr: ();
$wgr: ();
$halfGutter: $gutter / 2;
$resetGutter: $halfGutter;
$from:left;
$to:right;
@for $columns from 0 through $nc {
@if $columns > 0 {
$resetGutter: $gutter;
}
@if $columns != 16 {
$gutterLeftStart : strip-units(perc($resetGutter * $columns)) + strip-units(perc($c * $columns));
$gutterLeftEnd : $gutterLeftStart + strip-units(perc($halfGutter));
$gridLeft : $gutterLeftEnd;
$gridRight : $gridLeft + strip-units(perc($c));
$gutterRightStart : $gridRight;
$gutterRightEnd : $gutterRightStart + strip-units(perc($halfGutter));
$gutterLeftStartWK : $gutterLeftStart / 100;
$gutterLeftEndWK : $gutterLeftEnd / 100;
$gridLeftWK : $gridLeft / 100;
$gridRightWK : $gridRight / 100;
$gutterRightStartWK: $gutterRightStart / 100;
$gutterRightEndWK : $gutterRightEnd / 100;
$wgr: join($wgr, unquote("color-stop(#{$gutterLeftStartWK}, green)"), comma);
$wgr: join($wgr, unquote("color-stop(#{$gutterLeftEndWK}, green)"), comma);
$wgr: join($wgr, unquote("color-stop(#{$gridLeftWK}, red)"), comma);
$wgr: join($wgr, unquote("color-stop(#{$gridRightWK}, red)"), comma);
$wgr: join($wgr, unquote("color-stop(#{$gutterRightStartWK}, green)"), comma);
$wgr: join($wgr, unquote("color-stop(#{$gutterRightEndWK}, green)"), comma);
$gr: join($gr, unquote("green #{$gutterLeftStart}%"), comma);
$gr: join($gr, unquote("green #{$gutterLeftEnd}%"), comma);
$gr: join($gr, unquote("red #{$gridLeft}%"), comma);
$gr: join($gr, unquote("red #{$gridRight}%"), comma);
$gr: join($gr, unquote("green #{$gutterRightStart}%"), comma);
$gr: join($gr, unquote("green #{$gutterRightEnd}%"), comma);
}
}
$prefixes: (-webkit-, -moz-, -ms-, -o-);
background-image: -webkit-gradient(linear, #{$from} 50%, #{$to} 50%, #{$wgr});
@each $prefix in $prefixes {
background-image: #{$prefix}linear-gradient(#{$from}, #{$gr});
}
}
上面的 mixin 效果很好,虽然 jsfiddle 确实支持 sass,但似乎它要么已经过时,要么不接受一些参数,所以我在下面的演示中发布了结果。
演示:http: //jsfiddle.net/shannonhochkins/DRuSk/