我在scss文件中定义了不同的变量,我在一些scss文件中使用了这些变量。
_variables.scss
$light-theme: rgba(94,161,215,0.3);
$dark-theme: #5EA1D7;
$darker-theme: #57647A;
$very-dark-theme: #455061;我如何定义三组主题?类似于:
default-theme {
$light-theme: rgba(94,161,215,0.3);
$dark-theme: #5EA1D7;
$darker-theme: #57647A;
$very-dark-theme: #455061;
}
dark-theme {
$light-theme: black;
$dark-theme: brown;
$darker-theme: black;
$very-dark-theme: black;
}
light-theme {
$light-theme: black;
$dark-theme: brown;
$darker-theme: black;
$very-dark-theme: black;
}我想根据选定的主题改变价值观。例如,我有3个按钮,选择在它们上,将改变可变的颜色。
app.component.html
<button mat-raised-button (click)="onSetTheme('default-theme')">Default</button>
<button mat-raised-button (click)="onSetTheme('dark-theme')">Dark</button>
<button mat-raised-button (click)="onSetTheme('light-theme')">Light</button>app.component.ts
onSetTheme(theme) {
//TODO here I want to change the theme
}如何在onSetTheme()函数中更改主题。
谢谢!
发布于 2018-01-27 01:46:05
为什么我们不能在浏览器中动态地更改sass变量?
Sass是CSS的预处理器,它使开发过程中更容易编写样式规则。浏览器不会加载..scss/..sass文件;它将加载CSS --因此浏览器必须将..scss/..sass转换为CSS。
Sass变量仅是Sass文件中的变量。一旦转换成CSS,变量将被它们在编译时表示的值替换。
开发中的.scss:
body {
background: $dark-theme;
}浏览器加载的已编译CSS:
body {
background: black;
}您的onSetTheme函数是一个javascript函数,它将在浏览器中运行,并且无法访问更改sass变量,因为它们此时不存在。浏览器只加载已编译的CSS (它不会加载原始Sass文件和变量)。
如何在浏览器中动态更改网站主题?
切换CSS类
CSS变量(mdn)
发布于 2019-02-05 17:45:47
您可以在一起使用SASS变量和其他SASS元素以及CSS变量。
SASS
$body-margin: 0 10px;
:root {
--bg-color: #000;
--text-color: #FFF;
}
body {
background: var(--bg-color);
color: var(--text-color;
margin: $margin;
}JS
onSetTheme(theme) {
let bgColor, textColor;
switch(theme) {
case light:
bgColor = #FFF;
textColor = #000;
break;
case dark:
bgColor = #000;
textColor = #FFF;
break;
}
document.querySelector('body').style.setProperty('--bg-color', bgColor);
document.querySelector('body').style.setProperty('--text-color', textColor);
}https://stackoverflow.com/questions/48445522
复制相似问题