数学与插值变量?

Ida*_*zit 23 sass

考虑以下sass:

$font-size: 18;                 
$em: $font-size;

$column: $font-size * 3;        // The column-width of my grid in pixels
$gutter: $font-size * 1;        // The gutter-width of my grid in pixels

$gutter-em: #{$gutter / $em}em; // The gutter-width in ems.
$column-em: #{$column / $em}em; // The column-width in ems;

$foo = $gutter-em / 2; // This results in a value like "1em/2". :(
$bar = ($gutter-em / 2); // this doesn't work either, same as above.
Run Code Online (Sandbox Code Playgroud)

我如何生成一个$foo有效的,并且我可以在其他表达式中进一步重用?

Hau*_*eth 28

Sass不能对字符串执行arithemetic操作,只能连接.使用插值时,您创建的是一个看起来像数字的字符串:

@debug type-of(#{10px});         // string
@debug type-of('10px');          // string
@debug type-of(unquote('10px')); // string
@debug type-of(10px);            // number
Run Code Online (Sandbox Code Playgroud)

如果您想要一个数字,请不要使用插值,也不要使用引号.10要将整数(例如)转换为长度(例如10px),请使用乘法:

$gutter-em: ($gutter / $em) * 1em;
@debug type-of($gutter-em);      // number
Run Code Online (Sandbox Code Playgroud)