在Sass mixin中处理可选参数

在Sass mixin中处理可选参数,sass,mixins,Sass,Mixins,我刚开始胡说八道,所以请容忍我 我有这个mixin来处理px(对于旧IE)和rem中的字体大小声明,对于现代浏览器,它还可以进行很好的行高计算 @mixin font-size($font-size, $line-height-val: "", $sledge-hammer: "" /* $sledge-hammer is for an optional `!important` keyword */) { font-size: ($font-size)+px#{$sledge-h

我刚开始胡说八道,所以请容忍我

我有这个mixin来处理
px
(对于旧IE)和
rem
中的字体大小声明,对于现代浏览器,它还可以进行很好的
行高计算

@mixin font-size($font-size, $line-height-val: "", $sledge-hammer: "" /* $sledge-hammer is    for an optional `!important` keyword */) {
    font-size: ($font-size)+px#{$sledge-hammer};
    font-size: ($font-size / $base-font-size)+rem#{$sledge-hammer};
    @if $line-height-val == "" {
          line-height: ceil($font-size / $base-line-height) * ($base-line-height / $font-size);
    } @else {
          line-height: #{$line-height-val};
    }
}
它可以工作,但我觉得可选参数(
$line height val
$sledge hammer
)不是最理想的方法
$line height val
是必需的,因为有时我需要手动声明
行高
,而
$sledge hammer
是必需的,因为我需要声明
!重要信息
我的一些助手类上的关键字

90%的时间我只是像这样使用mixin:

@include font-size($font-size-sml, $sledgehammer: !important);
@mixin font-size($font-size, $line-height-val: "", $i: false /* flag for`!important` */) {
    $important: if($i, "!important", "");
    font-size: ($font-size)+px #{$important};
    font-size: ($font-size / $base-font-size)+rem #{$important};
    @if $line-height-val == "" {
          line-height: ceil($font-size / $base-line-height) * ($base-line-height / $font-size);
    } @else {
          line-height: #{$line-height-val};
    }
}
@包括字体大小(24)

其中也包括:

font-size: 24px;
font-size: 1.5rem;
line-height: 1.1;
font-size: 24px;
font-size: 1.5rem;
line-height: 1.6;
font-size: 15px!important;
font-size: 0.9375rem!important;
line-height: 1.6;
当我需要覆盖
行高度时,我会执行以下操作:

@包括字体大小(24,1.6)

其中也包括:

font-size: 24px;
font-size: 1.5rem;
line-height: 1.1;
font-size: 24px;
font-size: 1.5rem;
line-height: 1.6;
font-size: 15px!important;
font-size: 0.9375rem!important;
line-height: 1.6;
但是如果我需要声明
!重要的
关键字然后我必须这样做:

@包括字体大小($font size sml,“,!重要)

其中也包括:

font-size: 24px;
font-size: 1.5rem;
line-height: 1.1;
font-size: 24px;
font-size: 1.5rem;
line-height: 1.6;
font-size: 15px!important;
font-size: 0.9375rem!important;
line-height: 1.6;
但是感觉很有趣,我不得不对第二个参数使用空的
,而第三个参数的值总是
!重要信息
那应该在混音器中吗


我只是想知道是否有一种更简洁的方法来编写这个mixin?

当您这样调用它们时,可以指定参数:

@include font-size($font-size-sml, $sledgehammer: !important);
@mixin font-size($font-size, $line-height-val: "", $i: false /* flag for`!important` */) {
    $important: if($i, "!important", "");
    font-size: ($font-size)+px #{$important};
    font-size: ($font-size / $base-font-size)+rem #{$important};
    @if $line-height-val == "" {
          line-height: ceil($font-size / $base-line-height) * ($base-line-height / $font-size);
    } @else {
          line-height: #{$line-height-val};
    }
}
您可以这样缩短mixin参数:

@include font-size($font-size-sml, $sledgehammer: !important);
@mixin font-size($font-size, $line-height-val: "", $i: false /* flag for`!important` */) {
    $important: if($i, "!important", "");
    font-size: ($font-size)+px #{$important};
    font-size: ($font-size / $base-font-size)+rem #{$important};
    @if $line-height-val == "" {
          line-height: ceil($font-size / $base-line-height) * ($base-line-height / $font-size);
    } @else {
          line-height: #{$line-height-val};
    }
}

您可以在如下方式调用参数时指定这些参数:

@include font-size($font-size-sml, $sledgehammer: !important);
@mixin font-size($font-size, $line-height-val: "", $i: false /* flag for`!important` */) {
    $important: if($i, "!important", "");
    font-size: ($font-size)+px #{$important};
    font-size: ($font-size / $base-font-size)+rem #{$important};
    @if $line-height-val == "" {
          line-height: ceil($font-size / $base-line-height) * ($base-line-height / $font-size);
    } @else {
          line-height: #{$line-height-val};
    }
}
您可以这样缩短mixin参数:

@include font-size($font-size-sml, $sledgehammer: !important);
@mixin font-size($font-size, $line-height-val: "", $i: false /* flag for`!important` */) {
    $important: if($i, "!important", "");
    font-size: ($font-size)+px #{$important};
    font-size: ($font-size / $base-font-size)+rem #{$important};
    @if $line-height-val == "" {
          line-height: ceil($font-size / $base-line-height) * ($base-line-height / $font-size);
    } @else {
          line-height: #{$line-height-val};
    }
}

但我只会写
!重要信息
,虽然不太详细,但实际值始终是
!重要信息
所以应该在mixin中,但我似乎无法让它工作。同样,如果我像上面的例子那样调用mixin,我会得到一个错误,因为它需要第二个参数:
$line height val
,因此,我所说的空双引号感觉很有趣。错误是因为我编写了
$sledgehammer
而不是
$sledgehammer
。否则它编译的很好。谢谢你的工作,你能解释一下这部分主要做了什么吗?最后的空双引号:
$important:if($i,“!important,”)
如果
是标准Sass函数:。如果你熟悉其他语言,它就像一个三元运算符。但我只会写
!重要信息
,虽然不太详细,但实际值始终是
!重要信息
所以应该在mixin中,但我似乎无法让它工作。同样,如果我像上面的例子那样调用mixin,我会得到一个错误,因为它需要第二个参数:
$line height val
,因此,我所说的空双引号感觉很有趣。错误是因为我编写了
$sledgehammer
而不是
$sledgehammer
。否则它编译的很好。谢谢你的工作,你能解释一下这部分主要做了什么吗?最后的空双引号:
$important:if($i,“!important,”)
如果
是标准Sass函数:。如果你熟悉其他语言,它就像一个三元运算符。