【问题标题】:How to change the value of a varible in _variable.scss?如何更改 variable.scss 中变量的值?
【发布时间】:2016-05-14 16:31:05
【问题描述】:

我有一个有多种颜色的网站。 我在 _variable.scss $theme_color:red; 中定义了一个变量;

如何将该变量的值更改为 $theme_color:blue;在_variable.scss 中?当我将蓝色类添加到

有什么办法可以做到吗?

【问题讨论】:

  • 如果您有 Sass/SCSS 问题,那么为什么要在标签列表中添加 Less?
  • 它是因为即使使用 LESS 也能做到这一点我会很高兴
  • 那么也许你应该让你的问题更通用(或)在问题本身中说明这一点。否则,额外标签的存在会使用户感到困惑。
  • 好的。您有针对 Less 或 Sass 方式的解决方案吗?
  • 我的建议是为每个主题使用单独的变量文件,然后静态生成所需的类或类似this

标签: javascript jquery css sass


【解决方案1】:

在根文件 main.scss 中使用:

@import '_variable.scss'; // import variables file
$theme_color:blue; // new color

【讨论】:

    【解决方案2】:

    您需要使用!default 关键字。

    !default

    /* @file _variable.scss */
    $theme_color: red !default;
    $theme_shadow: 1px 1px 2px $theme_color !default;
    
    /* @file styles.scss */
    @import 'variable';
    .foo {
      color: $theme_color;
      box-shadow: $theme_shadow;
    }
    
    /* @file corporate.scss */
    // I override the color BEFORE the loading of my variables declaration.
    $theme_color: blue;
    @import 'variable';
    
    .foo {
      color: $theme_color;
      box-shadow: $theme_shadow;
    }    
    // Specific rule
    .corporate-thing {
      box-shadow: $theme_shadow;
    }
    

    CSS 输出

    /* @file styles.scss */
    .foo {
      color: red;
      box-shadow: 1px 1px 2px red;
    }
    
    /* @file corporate.scss */
    .foo {
      color: blue;
      box-shadow: 1px 1px 2px blue;
    }
    
    .corporate-thing {
      box-shadow: 1px 1px 2px blue;
    }
    

    没有!default

    如果不使用!default,则每次调用_variable.scss时都会覆盖该变量:

    /* @file _variable.scss */
    $theme_color: red;
    $theme_shadow: 1px 1px 2px $theme_color;
    
    /* @file styles.scss */
    @import 'variable';
    .foo {
      color: $theme_color;
      box-shadow: $theme_shadow;
    }
    
    /* @file corporate.scss */
    $theme_color: blue;
    
    // Now, $theme_color will be overwritten with the `red` value
    // because of the absence of the `!default` keyword.
    @import 'variable';
    
    .foo {
      color: $theme_color;
      box-shadow: $theme_shadow;
    }    
    // Specific rule
    .corporate-thing {
      box-shadow: $theme_shadow;
    }
    

    CSS 输出

    /* @file styles.scss */
    .foo {
      color: red;
      box-shadow: 1px 1px 2px red;
    }
    
    /* @file corporate.scss */
    .foo {
      color: red;
      box-shadow: 1px 1px 2px red;
    }    
    .corporate-thing {
      box-shadow: 1px 1px 2px red;
    }
    

    最终错误情况:加载后改变颜色_variable.scss

    /* @file _variable.scss */
    $theme_color: red;
    $theme_shadow: 1px 1px 2px $theme_color;
    
    /* @file styles.scss */
    @import 'variable';
    .foo {
      color: $theme_color;
      box-shadow: $theme_shadow;
    }
    
    /* @file corporate.scss */
    @import 'variable';
    $theme_color: blue; // AFTER
    
    .foo {
      color: $theme_color;
      // In this case, $theme_shadow is not overwritten with the new value
      // of $theme_color and keeps it value to 'red'.
      box-shadow: $theme_shadow;
    }    
    // Specific rule
    .corporate-thing {
      box-shadow: $theme_shadow;
    }
    

    CSS 输出

    /* @file styles.scss */
    .foo {
      color: red;
      box-shadow: 1px 1px 2px red;
    }
    
    /* @file corporate.scss */
    .foo {
      color: blue; // OK: blue
      box-shadow: 1px 1px 2px red; // KO: red
    }    
    .corporate-thing {
      box-shadow: 1px 1px 2px red; // KO: red
    }
    

    奖金

    其实可以直接从corporate.scss导入styles.scss

    /* @file _variable.scss */
    $theme_color: red !default;
    $theme_shadow: 1px 1px 2px $theme_color !default;
    
    /* @file styles.scss */
    @import 'variable';
    .foo {
      color: $theme_color;
      box-shadow: $theme_shadow;
    }
    
    /* @file corporate.scss */
    // I override the color BEFORE the loading of my main stylesheet
    $theme_color: blue;
    
    // Now I have to generate the same code that `style.css`, so the
    // easiest way to do it is to directly import it.
    @import 'styles';
    
    // Specific rule
    .corporate-thing {
      box-shadow: $theme_shadow;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 1970-01-01
      • 2014-03-19
      • 2015-11-09
      • 1970-01-01
      • 2017-09-04
      • 2021-06-29
      • 2022-12-05
      • 1970-01-01
      相关资源
      最近更新 更多