我需要使用不同颜色的图标在5种状态下设置按钮组件的样式,并在以下CSS中使用
#add-member-dialog #add-username-to-list .to-list-button-icon:before {
content: url("../images/func_personplus_16_ena.png");
}
#add-member-dialog #add-username-to-list:hover .to-list-button-icon:before {
content: url("../images/func_personplus_16_hov.png");
}
#add-member-dialog #add-username-to-list:disabled .to-list-button-icon:before {
content: url("../images/func_personplus_16_dis.png");
}
#add-member-dialog #add-username-to-list:active .to-list-button-icon:before {
content: url("../images/func_personplus_16_act.png");
}
#add-member-dialog #add-username-to-list:active:hover .to-list-button-icon:before {
content: url("../images/func_personplus_16_onb.png");
}
此类项目在与#add-username-to-list相关的伪类中有所不同。 我试图将整个css文件转换为scss,并想优化这种样式,但是我无法超越:
#add-member-dialog {
#add-username-to-list {
.to-list-button-icon:before {
content: url("../images/func_personplus_16_ena.png");
}
&:hover .to-list-button-icon:before {
content: url("../images/func_personplus_16_hov.png");
}
&:disabled .to-list-button-icon:before {
content: url("../images/func_personplus_16_dis.png");
}
&:active .to-list-button-icon:before {
content: url("../images/func_personplus_16_act.png");
}
&:active:hover .to-list-button-icon:before {
content: url("../images/func_personplus_16_onb.png");
}
}
}
有可能做这样的事情吗?
#add-member-dialog {
#add-username-to-list {
.to-list-button-icon:before {
content: url("../images/func_personplus_16_ena.png");
&&:hover & {
content: url("../images/func_personplus_16_hov.png");
}
...
}
}
}
其中&&
将代表祖父选择器#add-username-to-list
。
我也尝试应用模式#{$grandparent}:tmp $
,但是结果选择器看起来像这样:#add-member-dialog #add-username-to-list:tmp #add-member-dialog #add-username-to-list .to-list-button-icon:before
#add-member-dialog {
#add-username-to-list {
$grandparent: &;
.to-list-button-icon:before {
content: url("../images/func_personplus_16_ena.png");
#{$grandparent}:hover & {
content: url("../images/func_personplus_16_hov.png");
}
...
}
}
}
任何建议是否可行?
答案 0 :(得分:1)
您可以使用插值来打印您的祖父母选择器(请注意@ at-root):
#add-member-dialog {
#add-username-to-list {
$g: &; // grandparent
$p: '../images/func_personplus_16_'; // icon path
@at-root {
.to-list-button-icon:before {
#{$g} & { content: url(#{$p}ena.png); }
#{$g}:hover & { content: url(#{$p}hov.png); }
#{$g}:disabled & { content: url(#{$p}dis.png); }
#{$g}:active & { content: url(#{$p}act.png); }
#{$g}:active:hover & { content: url(#{$p}onb.png); }
}
}
}
}