Sass 基于类的变量值

Sass 基于类的变量值,sass,bootstrap-4,scss-mixins,Sass,Bootstrap 4,Scss Mixins,为什么不能基于类更改scss变量?在类暗模式下,我希望变量为绿色 Css: Html: 测试 我如何做到这一点?我不想要的是两个变量。这是因为变量作用域 在Sass中,在mixin或函数之外声明的所有变量都将具有全局作用域,并且可以在使用该变量的任何Sass选择器中引用。() 这意味着在mixin或函数中设置的任何变量值仅在该mixin或函数中可用,即使该变量以前是全局设置的 在不同的Sass变量集之间切换 分部 您可以为每个主题创建一个部分文件,并在每个主题的父类下导入这些文件 _the

为什么不能基于类更改scss变量?在类暗模式下,我希望变量为绿色

Css:

Html:


测试

我如何做到这一点?我不想要的是两个变量。

这是因为变量作用域

在Sass中,在mixin或函数之外声明的所有变量都将具有全局作用域,并且可以在使用该变量的任何Sass选择器中引用。()

这意味着在mixin或函数中设置的任何变量值仅在该mixin或函数中可用,即使该变量以前是全局设置的


在不同的Sass变量集之间切换 分部 您可以为每个主题创建一个部分文件,并在每个主题的父类下导入这些文件

_theme-dark.scss
$background-color: #000;
$text-color: #fff;
_theme-light.scss
$background-color: #fff;
$text-color: #000;
_主题-page.scss
body {
    background: $background-color;
    color: $text-color;
}
body {
    background: theme-value('background-color');
    color: theme-value('text-color');
}
theme-styles.scss
.theme-dark {
    @import "theme-dark";
    @import "themed-page";
}

.theme-light {
    @import "theme-light";
    @import "themed-page";
}
.theme-dark {
    $theme: 'dark';
    @import "themed-page";
}

.theme-light {
    $theme: 'light';
    @import "themed-page";
}

地图 另一个选项是将主题值存储在地图中,并使用实用程序函数检索所需的值。()

_theme-variables.scss
$theme: 'default';

$theme-values: ( 
    'background-color': ( 
        'default': #eee,
        'light': #fff,
        'dark': #000
    ),
    'text-color': ( 
        'default': #333,
        'light': #000,
        'dark': #fff
    )
);

@function theme-value($key) {
    $map: map-get($theme-values, $key);
    @return map-get($map, $theme);
}
_主题-page.scss
body {
    background: $background-color;
    color: $text-color;
}
body {
    background: theme-value('background-color');
    color: theme-value('text-color');
}
theme-styles.scss
.theme-dark {
    @import "theme-dark";
    @import "themed-page";
}

.theme-light {
    @import "theme-light";
    @import "themed-page";
}
.theme-dark {
    $theme: 'dark';
    @import "themed-page";
}

.theme-light {
    $theme: 'light';
    @import "themed-page";
}

这是因为变量作用域

在Sass中,在mixin或函数之外声明的所有变量都将具有全局作用域,并且可以在使用该变量的任何Sass选择器中引用。()

这意味着在mixin或函数中设置的任何变量值仅在该mixin或函数中可用,即使该变量以前是全局设置的


在不同的Sass变量集之间切换 分部 您可以为每个主题创建一个部分文件,并在每个主题的父类下导入这些文件

_theme-dark.scss
$background-color: #000;
$text-color: #fff;
_theme-light.scss
$background-color: #fff;
$text-color: #000;
_主题-page.scss
body {
    background: $background-color;
    color: $text-color;
}
body {
    background: theme-value('background-color');
    color: theme-value('text-color');
}
theme-styles.scss
.theme-dark {
    @import "theme-dark";
    @import "themed-page";
}

.theme-light {
    @import "theme-light";
    @import "themed-page";
}
.theme-dark {
    $theme: 'dark';
    @import "themed-page";
}

.theme-light {
    $theme: 'light';
    @import "themed-page";
}

地图 另一个选项是将主题值存储在地图中,并使用实用程序函数检索所需的值。()

_theme-variables.scss
$theme: 'default';

$theme-values: ( 
    'background-color': ( 
        'default': #eee,
        'light': #fff,
        'dark': #000
    ),
    'text-color': ( 
        'default': #333,
        'light': #000,
        'dark': #fff
    )
);

@function theme-value($key) {
    $map: map-get($theme-values, $key);
    @return map-get($map, $theme);
}
_主题-page.scss
body {
    background: $background-color;
    color: $text-color;
}
body {
    background: theme-value('background-color');
    color: theme-value('text-color');
}
theme-styles.scss
.theme-dark {
    @import "theme-dark";
    @import "themed-page";
}

.theme-light {
    @import "theme-light";
    @import "themed-page";
}
.theme-dark {
    $theme: 'dark';
    @import "themed-page";
}

.theme-light {
    $theme: 'light';
    @import "themed-page";
}