简体   繁体   English

为什么不能更新CSS变量名称以应用不同的颜色主题?

[英]Why can't update css variable name to apply a different color theme?

I want to switch between two css themes in an else/if statement built in scss by changing a single custom variable via javascript. 我想通过使用javascript更改单个自定义变量来在内置于scss中的else / if语句中的两个CSS主题之间切换。

I see lots of examples out there about how change one or two colors in css variables via javascript, but I want to change the whole theme of a site by changing a $theme variable only. 我看到了很多示例,这些示例说明如何通过javascript更改CSS变量中的一种或两种颜色,但是我只想通过更改$ theme变量来更改网站的整个主题。

Check the codepen: https://codepen.io/roroland/pen/gVWLBm 检查代码笔: https ://codepen.io/roroland/pen/gVWLBm

// Var defaults
$theme: default;
$primary: null;
$warn: null;
:root {
  --theme: #{$theme};
}

// If theme is 'default' use this
@if ( $theme == "default") {
  $primary: orange;
  $warn: purple;
}
// If theme is 'other' use this instead
@elseif($theme == "other") {
  $primary: black;
  $warn: blue;
}

p {
  display: block;
  padding: 2rem;
  color: $primary;
  background: $warn;
}

JS JS

document.documentElement.style.setProperty('--theme', "other");

If I try to update $theme it doesn't work, I tried with and without interpolation, setting 'null' the theme var, etc.. it just ignore the JS instruction. 如果我尝试更新$ theme则不起作用,则尝试使用和不使用插值,将主题var设置为“ null”等。它只是忽略了JS指令。

Can someone explain why isn't working? 有人可以解释为什么不起作用吗? Thanks 谢谢

I think what you are trying to do, can not be done. 我认为您正在尝试做的事情,无法完成。

SCSS is a CSS preprocessor which runs on the server-side. SCSS是在服务器端运行的CSS预处理器。 It would compile into CSS and then be rendered in the client-side (browser). 它将被编译成CSS,然后在客户端(浏览器)中呈现。 And once it is compiled to CSS, all the SCSS variables are gone (Because the browser can only render CSS). 并且一旦将其编译为CSS,所有的SCSS变量都将消失(因为浏览器只能呈现CSS)。

The JS which you are writing is being executed in the browser (client-side). 您正在编写的JS正在浏览器(客户端)中执行。

So, if you want this to be done, you need to either 因此,如果您希望做到这一点,则需要

  • Assign classes to elements and style them. 为元素分配类并设置其样式。 Then, change the Class using JS 然后,使用JS更改类
document.getElementById("MyElement").className = "other-theme";
document.getElementById("MyElement").className = "default-theme";

And in CSS, 在CSS中

.other-theme{
color: orange;
background: purple;
}

.default-theme{
color: black;
background: blue;
}

You may use SCSS for this CSS style generation. 您可以将SCSS用于此CSS样式生成。

  • Or, you may use an approach with AJAX which sends requests to the server and gets the updated style sheet. 或者,您可以对AJAX使用一种将请求发送到服务器并获取更新的样式表的方法。

This is a job for CSSVariables (dynamic) that unlike Sass variables (static) works at run-time. 这是CSSVariables(动态)的工作,与Sass变量(静态)不同,它在运行时起作用。

 /* toggle theme class */ addEventListener('click', e => { document.documentElement.classList.contains('other') ? document.documentElement.classList.remove('other') : document.documentElement.classList.add('other'); }) 
 /* default theme */ :root { --primary: orange; --warn: purple; } /* other theme */ :root.other { --primary: black; --warn: blue; } p { display: block; padding: 2rem; /* use theme colors */ color: var(--primary); background: var(--warn); } 
 <h2>Click to toggle theme class</h2> <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Distinctio dolorem quas quod eaque voluptatem error, rem vero non. Eveniet maxime saepe fugiat tenetur dignissimos enim, provident earum illo quasi fugit?</p> 

I think you should implement javascript rather than complicated css by adding an event listener and toggle the class (have two different classes) 我认为您应该通过添加事件侦听器并切换类(具有两个不同的类)来实现javascript而不是复杂的CSS

This link might help 此链接可能有帮助

https://www.w3schools.com/howto/howto_js_toggle_class.asp https://www.w3schools.com/howto/howto_js_toggle_class.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM