【问题标题】:Can I use psuedo elements as an argument to a scss mixin?我可以使用伪元素作为 scss mixin 的参数吗?
【发布时间】:2018-11-23 04:01:55
【问题描述】:

我有一个 mixin,我引用一个“元素”作为参数。

@mixin themeParent ($child) {
    &--blue #{$child} {
        color: getColour("theme", "bluehighlight");
    }

    &--green #{$child}  {
        color: getColour("theme", "greenhighlight");
    }

    &--purple #{$child}  {
        color: getColour("theme", "purplehighlight");
    }

    &--gossamer #{$child}  {
        color: getColour("theme", "gossamerhighlight");
    }

    &--black #{$child}  {
        color: getColour("theme", "black");
    }
}

例如,如果我引用 ap,这可以正常工作

HTML

<div class="div--blue">
    <a>blue link</a>
</div>

SCSS

div {
    @include themeParent(a);
}

但我也想将 mixin 用于伪元素,例如。

div {
    @include themeParent(a:hover);
}

 div {
        @include themeParent(>a:hover);
    }


 div {
        @include themeParent(&:first-child);
    }

这可能吗? 为什么我正在做的事情让 SCSS 不开心:-(

【问题讨论】:

    标签: sass mixins scss-mixins


    【解决方案1】:

    你只需要把你的论点放在一个字符串中。

    DEMO

    @mixin themeParent($child) {
        &--blue #{$child} {
            color: blue;
        }
    }
    
    .cool {
      @include themeParent('a:hover');
      @include themeParent('&:first-child');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 2016-12-05
      • 2013-11-01
      • 2021-04-25
      • 1970-01-01
      相关资源
      最近更新 更多