2

当您想修改函数范围之外的变量时,是否有任何解决方法?:

$ui-theme: 'pink';

@if $ui-theme == "pink" {
    $ui-main: #eb82b0;  
    $ui-secondary: #fff5f9;
    $ui-third: #f6c2d9;
} @else {
    $ui-main: #ff8067;  
    $ui-secondary: #fff6e5;
    $ui-third: #ffb1a2;
}

a { color: $ui-main; background: $ui-secondary };

我想创建一个名为的全局变量ui-theme,它将$ui-main, $ui-secondary...在定义下方编写的所有其他代码中进行定义。似乎在使用诸如@if.

有谁知道如何做到这一点?

4

1 回答 1

3

您必须在控制块之外初始化变量:

$ui-theme: 'pink';

$ui-main: null;
$ui-secondary: null;
$ui-third: null;

@if $ui-theme == "pink" {
    $ui-main: #eb82b0;  
    $ui-secondary: #fff5f9;
    $ui-third: #f6c2d9;
} @else {
    $ui-main: #ff8067;  
    $ui-secondary: #fff6e5;
    $ui-third: #ffb1a2;
}

a { color: $ui-main; background: $ui-secondary };

输出:

a {
  color: #eb82b0;
  background: #fff5f9;
}

通过@include像这样的硬编码变量而不是硬编码变量可能会更好。

_pink.scss:

$ui-main: #eb82b0;  
$ui-secondary: #fff5f9;
$ui-third: #f6c2d9;

样式.scss:

@include "pink"; // or don't include anything if you want the default colors

$ui-main: #ff8067 !default;
$ui-secondary: #fff6e5 !default;
$ui-third: #ffb1a2 !default;

a { color: $ui-main; background: $ui-secondary };
于 2013-05-24T20:13:51.393 回答