admin管理员组文章数量:1320781
I'm trying to implement switchable themes for a project, it's a Mendix project, but let's suppose it's a website.
- There are many different pages, and different elements on each page.
- All styles for them are written in SASS. The pages and elements use values from one separate SASS file with variables - variables-1.
- I have a second SASS file with the same variables but different values - variables-2.
Problem:
I need to change the color theme of the website on a button click. So basically I need all the elements to take colors from the different set of variables when I click a button.
I see two options here:
Compile 2 css files and switch between them: link all sass files to variables-1, pile CSS file, then link all sass files to variables-2, pile second css file. Switch between them on a button click.
In this option, colors won't be assigned dynamically, and every time when I make changes I will have to manually link each sass file to different variables, or change variables values, and repile everything. Also I won't be able to continue working on the project and switch themes at the same time.
Predefine color themes in mixins, pile css classes for each theme like in this article, and use some custom js logic to assign corresponding classes to the elements.
Most likely won't work, since there are too many elements in the project, and would be too plicated to change class for each one.
How can I solve this problem?
I'm trying to implement switchable themes for a project, it's a Mendix project, but let's suppose it's a website.
- There are many different pages, and different elements on each page.
- All styles for them are written in SASS. The pages and elements use values from one separate SASS file with variables - variables-1.
- I have a second SASS file with the same variables but different values - variables-2.
Problem:
I need to change the color theme of the website on a button click. So basically I need all the elements to take colors from the different set of variables when I click a button.
I see two options here:
Compile 2 css files and switch between them: link all sass files to variables-1, pile CSS file, then link all sass files to variables-2, pile second css file. Switch between them on a button click.
In this option, colors won't be assigned dynamically, and every time when I make changes I will have to manually link each sass file to different variables, or change variables values, and repile everything. Also I won't be able to continue working on the project and switch themes at the same time.
Predefine color themes in mixins, pile css classes for each theme like in this article, and use some custom js logic to assign corresponding classes to the elements.
Most likely won't work, since there are too many elements in the project, and would be too plicated to change class for each one.
How can I solve this problem?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 26, 2019 at 22:14 RumataRumata 1,0473 gold badges21 silver badges51 bronze badges 2- Perhaps you could do something smart with CSS custom properties – Webber Commented Mar 26, 2019 at 22:23
- make css parsed by php or other language - where u replace colors, or make second css class for body, and only change body class='' – Grzegorz Miśkiewicz Commented Mar 26, 2019 at 23:48
1 Answer
Reset to default 3You can't dynamically change sass variables in the browser. Here is a more detailed explanation:
Changing variables values scss
A possible solution for you is to add or remove a class to the body that represents the theme. Then, for example, if body has class "dark-theme", background color is black and so on.
本文标签: javascriptHow to dynamically switch color themes using SASSStack Overflow
版权声明:本文标题:javascript - How to dynamically switch color themes using SASS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742090368a2420228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论