根据您还想用这个选择器做什么,我会这样做:
@mixin arrowPlacement($placement) {
&[data-arrow-placement^="#{$placement}"] {
[data-arrow] {
@content;
}
}
}
.dialog {
$arrowPosition: -12px;
@include arrowPlacement(top) {
bottom: $arrowPosition;
};
@include arrowPlacement(bottom) {
top: $arrowPosition;
};
@include arrowPlacement(left) {
right: $arrowPosition;
};
@include arrowPlacement(bottom-right) {
left: $arrowPosition;
top: $arrowPosition;
};
}
或者像这样(如果需要,您甚至可以添加@content):
@mixin arrowPlacement($placement, $position, $properties...) {
&[data-arrow-placement^="#{$placement}"] {
[data-arrow] {
@each $property in $properties {
#{$property}: $position;
}
}
}
}
.dialog {
$arrowPosition: -12px;
@include arrowPlacement(top, $arrowPosition, bottom);
@include arrowPlacement(bottom, $arrowPosition, top);
@include arrowPlacement(left, $arrowPosition, right);
@include arrowPlacement(bottom-right, $arrowPosition, left, top);
}
而且如果你不打算重用mixin,你可以直接使用-12px位置值,而不是作为参数传递:
@mixin arrowPlacement2($placement, $properties...) {
&[data-arrow-placement^="#{$placement}"] {
[data-arrow] {
@each $property in $properties {
#{$property}: -12px;
}
}
}
}
.dialog {
@include arrowPlacement2(top, bottom);
@include arrowPlacement2(bottom, top);
@include arrowPlacement2(left, right);
@include arrowPlacement2(bottom-right, left, top);
}
argument list (...) 可以让您传递任意数量的属性。