【问题标题】:Create A Mixin With and Without Border创建一个有边界和无边界的 Mixin
【发布时间】:2015-01-09 20:45:31
【问题描述】:

我正在尝试创建一个 Mixin,它从两个变量中提取一个并创建一个填充按钮,或者根据传递的变量创建一个轮廓按钮。

@include button-style($color: red);

// would result in
background-color: transparent;
color: red;
box-shadow: inset 0 0 0 1px red;

@include button-style($bg: red);

// would result in
background-color: red;
color: white;

有没有办法做到这一点?我在这里发疯了,试图找出实现这一目标的最简单方法。这是我到目前为止所得到的。

@mixin button-style($bg: transparent, $color: white) {
  background-color: $bg;
  color: $color;
  @if $color == 'white' {
    box-shadow: inset 0 0 0 1px $color;
  }
}

感谢任何帮助。提前致谢!

【问题讨论】:

    标签: css sass mixins


    【解决方案1】:

    添加一个额外的参数并对其进行检查。

    @mixin button-style($bg: transparent, $color: white, $border: true) {
      background-color: $bg;
      color: $color;
      @if $border {
        box-shadow: inset 0 0 0 1px $color;
      }
    }
    
    .foo {
      @include button-style;
    }
    
    .bar {
      @include button-style($border: false);
    }
    

    输出:

    .foo {
      background-color: transparent;
      color: white;
      box-shadow: inset 0 0 0 1px white;
    }
    
    .bar {
      background-color: transparent;
      color: white;
    }
    

    或者,您可以使用空值:

    @mixin button-style($bg: transparent, $color: white, $border: inset 0 0 0 1px $color) {
      background-color: $bg;
      color: $color;
      box-shadow: $border;
    }
    
    .foo {
      @include button-style;
    }
    
    .bar {
      @include button-style($border: null);
    }
    

    【讨论】:

    • 如果可能的话,我宁愿在@include 中不要有额外的参数?也许不可能……
    • 要么添加额外的参数,要么将其绑定到其他参数之一,以便必须设置背景或颜色才能获得边框。还有第 3 个选项,但它更冗长。
    • Mixin if/else 语句可以带字符串吗?例如,@if $color == 'white'??
    • 你为什么要问我这个而不是自己测试?您本可以为自己节省一些时间。
    • 所以我在这里解决了这个问题:sassmeister.com/gist/f1079496e74af06ff5f1 唯一的缺点是我必须使用一个名为$null 的变量。如果可能,我宁愿不使用该变量并像这样引用它:@if $bg == 'null'... 这可能吗?
    【解决方案2】:

    这似乎对我有用。我已经设置了working example over here。唯一的缺点是我必须将 transparent 绑定到一个变量,如下所示:

    $transparent: transparent;
    
    @mixin button-style($bg: $transparent, $color: white) {
      background-color: $bg;
      color: $color;
      @if $bg == $transparent {
        box-shadow: inset 0 0 0 1px $color;
      }
    }
    
    .button-pri {
      @include button-style($bg: red);
    }
    
    .button-sec {
      @include button-style($color: red);
    }
    

    如果可能的话,我想从等式中删除该变量并直接使用if $bg == 'transparent { ...,但if 语句似乎不适用于字符串。

    更新

    感谢@KreaTief,显然我不需要使用变量。下面更新了答案:

    @mixin button-style($bg: transparent, $color: white) {
      background-color: $bg;
      color: $color;
      @if $bg == transparent {
        box-shadow: inset 0 0 0 1px $color;
      }
    }
    
    .button-pri {
      @include button-style($bg: red);
    }
    
    .button-sec {
      @include button-style($color: red);
    }
    

    【讨论】:

    猜你喜欢
    • 2013-02-10
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    相关资源
    最近更新 更多