@mixin setMargin($direction, $value) {
margin-#{$direction}: #{$value}px;
}
@for $i from 1 through 100 {
$topSelector: unquote("mar-top#{$i}");
.#{$topSelector} {
@include setMargin(top, $i);
}
$bottomSelector: unquote("mar-bottom#{$i}");
.#{$bottomSelector} {
@include setMargin(bottom, $i);
}
$leftSelector: unquote("mar-left#{$i}");
.#{$leftSelector} {
@include setMargin(left, $i);
}
$rightSelector: unquote("mar-right#{$i}");
.#{$rightSelector} {
@include setMargin(right, $i);
}
}
@mixin setMargin($direction, $value) {
padding-#{$direction}: #{$value}px;
}
@for $i from 1 through 100 {
$topSelector: unquote("pad-top#{$i}");
.#{$topSelector} {
@include setMargin(top, $i);
}
$bottomSelector: unquote("pad-bottom#{$i}");
.#{$bottomSelector} {
@include setMargin(bottom, $i);
}
$leftSelector: unquote("pad-left#{$i}");
.#{$leftSelector} {
@include setMargin(left, $i);
}
$rightSelector: unquote("pad-right#{$i}");
.#{$rightSelector} {
@include setMargin(right, $i);
}
}
在html中用法是
<div class="mar-top20 pad-top20">边距</div>
@for $i from 10 through 50 {
.fontS#{$i}px {
font-size: #{$i}px;
}
}
$colors: (
"900": #900,
"f00": #f00,
"0f0": #0f0,
"00f": #00f
);
@each $name, $color in $colors {
.cl##{$name} {
color: #{$color};
}
}
在html中用法是
<p class="fontS20px cl#900">字体大小是20px,颜色是#900</p>
<p class="fontS15px cl#f00">字体大小是15px,颜色是#f00</p>