如何更改_variable.scss中变量的值
How to change the value of a varible in _variable.scss?
我有一个有多种颜色的网站。我在_variable.scss$theme_color:red中定义了一个变量;
如何将该变量的值更改为$theme_color:blue;在_variable.scss中?而我把蓝色添加到
有什么可能的办法吗?
在根文件main.scss中使用:
@import '_variable.scss'; // import variables file
$theme_color:blue; // new color
您需要使用!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;
}
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 全局变量和全局对象的属性之间有什么区别吗
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 值对象在某个变量发生更改后发生更改
- Javascript变量赋值|
- AngularJS-在JSON选择器中使用变量名
- Javascript计数器变量未显示正确的值
- delete关键字在全局变量上的不同行为
- 如何在php变量中嵌入JQuery代码
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 通过Ember颜色选择器更新SCSS变量
- 如何更改_variable.scss中变量的值
- 瞄准JS中的SCSS变量