【问题标题】:how to use grandparent selector in scss如何在scss中使用祖父母选择器
【发布时间】:2019-11-14 17:15:08
【问题描述】:

我需要在 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");
      }
      ...
    }
  }
}

任何建议这是否可能?

【问题讨论】:

  • 刚刚开始您的父级选择器概念。你必须找到另一种方式

标签: css sass


【解决方案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); }                                
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多