Front-end theme color dynamic switching method and system
Through the combination of LESS and Vite, combined with Pinia or Redux state management library, it solves the problem of combining CSS and JavaScript, realizes dynamic theme color switching, ensures that the theme color of third-party pages is updated simultaneously, and improves the user experience.
Patent Information
- Application Number
- CN202410721145.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-05
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2044-06-05
AI Technical Summary
The existing technology cannot combine CSS and JavaScript to dynamically change the background color of website pages, buttons, tables, and link font colors, and it cannot synchronously update the theme color of third-party pages embedded in iframes.
Create theme color files through LESS preprocessor, use Vite to reference CSS variables in Vue projects, monitor page URL parameters, dynamically set theme color style files through routing guards, and use Pinia or Redux state management library to manage theme colors to ensure chart library Components such as Echarts change according to the theme color.
It realizes the combination of CSS and JavaScript, quickly and accurately switches the theme color dynamically, and ensures that the theme color of the main frame and the embedded page is consistent, which greatly improves the user experience.
Smart Images

Figure CN118689573A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of front-end development, and in particular to a method and system for dynamically switching front-end theme colors. Background Art
[0002] As website page content becomes increasingly rich and interactions become more complex, websites are gradually adding some personalized customization features, such as theme color switching.
[0003] Apache ECharts is an open source visualization chart library based on JavaScript. ECharts provides a variety of graphics drawing, such as line charts, bar charts, etc., and provides a variety of custom configurations for users to configure chart styles. The custom configuration is injected into the Options of Echarts through JavaScript variables. The dynamic switching of the theme color designed this time requires the use of JavaScript combined with CSS variables.
[0004] The theme color replacement on the market mainly uses CSS variables for identification, which is used to replace the styles of some components in the page. However, for apps like Echarts that use JavaScript to configure the style, it is impossible to perform synchronous theme color replacement.
[0005] How to combine CSS and JavaScript to dynamically change the background color, buttons, tables, font color of links, etc. in the main page, while also being able to control third-party pages embedded in iframes, is a technical problem that needs to be solved. Summary of the invention
[0006] The technical task of the present invention is to address the above shortcomings and provide a front-end theme color dynamic switching method and system to solve the technical problem of how to combine CSS and JavaScript so that third-party pages embedded in iframe can also change the background color and font color according to the theme color.
[0007] In a first aspect, the present invention provides a method for dynamically switching a front-end theme color, comprising the following steps:
[0008] Theme color file configuration: Create a theme color file through the LESS preprocessor to define light theme colors and dark theme colors;
[0009] Theme color variable reference: In the Vue project, use Vite to configure the global theme file color file and reference the theme color variable CSS;
[0010] Theme color loading: listen to the parameters in the page URL, dynamically set the theme color style file to dark theme or light theme through the route guard, manage the theme color through Pinia or Redux state management library, and use it globally. If a third-party library including Echarts is used in the Vue project, obtain and listen to the global theme as dark or light to redraw the chart.
[0011] Preferably, the theme color file configuration includes the following steps:
[0012] Configure the Vite configuration file. The default configuration of the additionalData field is the bright theme file.
[0013] Compile and execute the Less file through Vite, write the light theme file light.module.less through Less, configure the light theme color, and write the dark theme file dark.module.less through Less, configure the dark theme color.
[0014] Preferably, the theme color variable reference includes the following steps:
[0015] Configure the light.module.less file to be referenced globally, and import the dark theme file through the additionalData field;
[0016] Compile and execute the vue file through Vite, and CSS variables can be referenced in the style tag and script tag of the vue file.
[0017] Preferably, the theme color loading includes the following steps:
[0018] Use the route parameter theme to determine whether the theme color is dark or light.
[0019] Create a global store object and export the useGlobalStore method. When the routing parameters change, dynamically update the global store object and set CSS variables. CSS variables can directly use theme color variables.
[0020] Change the CSS variables referenced in the vue file. CSS variables can be referenced in both the style tag and the script tag of the vue file.
[0021] Get the switched theme color variable in JavaScript;
[0022] In the chart library Echarts, the current theme color is obtained through the theme parameter of the global globalStore.
[0023] In a second aspect, the present invention provides a front-end theme color dynamic switching system, which is used to implement the front-end theme color dynamic switching through a front-end theme color dynamic switching method as described in any one of the first aspects, and the system includes a theme color file configuration module, a theme color variable reference module, and a theme color loading module;
[0024] The theme color file configuration module is used to perform the following: create a theme color file through the LESS preprocessor, define the light theme color and the dark theme color;
[0025] The theme color variable reference module is used to perform the following: configure the global theme file color file through Vite in the Vue project and reference the theme color variable CSS;
[0026] The theme color loading module is used to perform the following: listen to the parameters in the page URL, dynamically set the theme color style file to a dark theme or a light theme through the routing guard, manage the theme color through the Pinia or Redux state management library, and use it globally. If a third-party library including Echarts is used in the Vue project, obtain and listen to the global theme as dark or light to redraw the chart.
[0027] Preferably, the theme color file is configured to perform the following:
[0028] Configure the Vite configuration file. The default configuration of the additionalData field is the bright theme file.
[0029] Compile and execute the Less file through Vite, write the light theme file light.module.less through Less, configure the light theme color, and write the dark theme file dark.module.less through Less, configure the dark theme color.
[0030] Preferably, the theme color variable reference module is used to perform the following:
[0031] Configure the light.module.less file to be referenced globally, and import the dark theme file through the additionalData field;
[0032] Compile and execute the vue file through Vite, and CSS variables can be referenced in the style tag and script tag of the vue file.
[0033] Preferably, the theme color loading module is used to perform the following:
[0034] Use the route parameter theme to determine whether the theme color is dark or light.
[0035] Create a global store object and export the useGlobalStore method. When the routing parameters change, dynamically update the global store object and set CSS variables. CSS variables can directly use theme color variables.
[0036] Change the CSS variables referenced in the vue file. CSS variables can be referenced in both the style tag and the script tag of the vue file.
[0037] Get the switched theme color variable in JavaScript;
[0038] In the chart library Echarts, the current theme color is obtained through the theme parameter of the global globalStore.
[0039] The front-end theme color dynamic switching method and system of the present invention have the following advantages:
[0040] 1. Use CSS variables and JavaScript to achieve fast and accurate dynamic switching of theme colors, simplifying the cumbersome code operation of dynamic theme color change function;
[0041] 2. Friendly to embedded third-party pages, ensuring that after the main frame theme color is changed, the related pages can also be updated synchronously, avoiding the problem of inconsistent theme colors of different pages;
[0042] 3. Greatly improves the user experience, allowing users to use any page on the platform without distinction after switching themes. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0044] The present invention is further described below in conjunction with the accompanying drawings.
[0045] Figure 1 This is a schematic diagram of the overall framework of an application platform for a front-end theme color dynamic switching method according to Example 1;
[0046] Figure 2 Example 1 is a flowchart of a method for dynamically switching the front-end theme color. DETAILED DESCRIPTION
[0047] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments so that those skilled in the art can better understand the present invention and implement it. However, the embodiments are not intended to limit the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments may be combined with each other.
[0048] The embodiment of the present invention provides a front-end theme color dynamic switching method and system, which is used to solve the technical problem of how to combine CSS and JavaScript so that a third-party page embedded in an iframe can also change the background color and font color according to the theme color.
[0049] Embodiment 1:
[0050] The invention discloses a front-end theme color dynamic switching method, which comprises three steps of theme color file configuration, theme color variable reference and theme color loading.
[0051] Step S100: Theme color file configuration: Create a theme color file through the LESS preprocessor to define a light theme color and a dark theme color.
[0052] The specific implementation of the theme color file configuration includes the following operations:
[0053] (1) Configure the Vite configuration file. The default configuration of the additionalData field is the bright theme file;
[0054] (2) Compile and execute the Less file through Vite, write the light theme file light.module.less through Less, configure the light theme color, and write the dark theme file dark.module.less through Less, configure the dark theme color.
[0055] Step S200: Theme color variable reference: In the Vue project, global theme color file configuration is performed through Vite, and the theme color variable CSS is referenced.
[0056] The specific implementation of the theme color variable reference includes the following operations:
[0057] (1) Configure the light.module.less file to be referenced globally and import the dark theme file through the additionalData field;
[0058] (2) Compile and execute the vue file through Vite. CSS variables can be referenced in the style tag and script tag of the vue file.
[0059] Step S300 theme color loading: monitor the parameters in the page URL, dynamically set the theme color style file to a dark theme or a light theme through the routing guard, manage the theme color through the Pinia or Redux state management library, and use it globally. If a third-party library including Echarts is used in the Vue project, obtain and monitor the global theme to be dark or light to redraw the chart.
[0060] The specific implementation of theme color loading includes the following steps:
[0061] (1) Use the route parameter theme to determine whether to use a dark theme or a light theme;
[0062] (2) Create a global store object and export the useGlobalStore method. When the routing parameters change, dynamically update the global store object and set CSS variables. CSS variables can directly use theme color variables.
[0063] (3) Change the CSS variables referenced in the vue file. CSS variables can be referenced in both the style tag and the script tag of the vue file.
[0064] (4) Get the theme color variable after switching in JavaScript;
[0065] (5) In the chart library Echarts, get the current theme color through the theme parameter of the globalStore.
[0066] CSS (Cascading Style Sheets) is a computer language used to express the style of files such as HTML (an application of Standard Generalized Markup Language) or XML (a subset of Standard Generalized Markup Language). CSS can not only modify web pages statically, but also dynamically format the elements of web pages in conjunction with various scripting languages.
[0067] Less is a CSS preprocessing language that expands the CSS language and adds features such as variables, mixins, and functions, making CSS easier to maintain, create themes, and expand. Based on the characteristics of Less, we can use CSS variables in the page to uniformly configure the theme color.
[0068] Vue is a JavaScript framework for building user interfaces. As a front-end framework used in daily front-end development, it greatly reduces the number of page elements that developers need to modify directly, and through the two-way binding feature, changes made by users on the platform can directly trigger changes to page element styles.
[0069] Vite is a front-end development and construction tool that can compile .vue files into JavaScript code that can be executed by the browser for parsing and rendering.
[0070] The method of this embodiment is based on the above main technology to implement a method for dynamically switching the front-end theme color. A specific implementation step is now given, including four operations: creating a light theme file, creating a dark theme file, referencing theme color variables, and loading theme color files on demand.
[0071] Operation 1: Create a light theme file: Use less to write a light theme file. First, create src / style / light.module.less and configure the light theme color. The code is as follows:
[0072] / / Light color theme
[0073] @color-primary:#0a8cfa;
[0074] :export{
[0075] --color-primary:@color-primary;
[0076] }
[0077] The operation is to create a dark theme file: Use less to write a dark theme file. First, create src / style / dark.module.less and configure the dark theme color. The light theme and dark theme are only distinguished by color values. The variable name remains unchanged, which is color-primary. The specific code is as follows:
[0078] / / Dark theme
[0079] @color-primary:#000;
[0080] :export{
[0081] --color-primary:@color-primary;
[0082] }
[0083] Operation 3: Theme color variable reference: Through Vite's configuration file Vite.config.js, it specifically includes the following two processes:
[0084] Process 1: Configure the light.module.less file for global reference and import the dark theme Less file through the additionalData field. The specific code is as follows:
[0085]
[0086] Process 2: After configuration, in the .vue file <style>标签和<script>标签中均可使用,具体代码如下:
[0087] <style>
[0088] .box{
[0089] color:@color-primary;
[0090] }
[0091] < / style>
[0092] <script setup>
[0093] import cssVar from'@ / style / light.module.less'
[0094] const primaryColor=cssVar['--color-primary'] / / 输出#0a8cfa
[0095] < / script> .
[0096] Operation 4: Load the theme color file on demand, including the following six processes.
[0097] Process 1: Get the theme parameter dark through routing. For example, if the routing address is xxx? theme=dark, you can get the dark theme through the parameter theme.
[0098] Process 2: Create a global store object / src / store / global.js, export the useGlobalStore method, and switch the global state in time when the routing parameters change. The code is as follows:
[0099] import{defineStore}from'pinia'
[0100] export const useGlobalStore=defineStore('global',{
[0101] state:()=>{
[0102] return {
[0103] theme:{}, / / Theme color configuration
[0104] themeName:'light' / / Theme color name
[0105] }
[0106] },
[0107] actions:{
[0108] / / When the routing parameters change, switch the global status in time
[0109] setTheme(theme, themeName){
[0110] this.theme = theme
[0111] this.themeName = themeName
[0112] }
[0113] }
[0114] })
[0115] Process 3: Make a judgment at the router.beforeEach global front guard hook and define the route file. For example, in the file / src / route / route.js, write the following code:
[0116] import{createRouter,createWebHashHistory}from'vue-router'
[0117] import{createPinia}from'pinia'
[0118] import{useGlobalStore}from'@ / store / global'
[0119] const routes = [
[0120] / / ... ]
[0122] / / Create a route
[0123] export const router=createRouter({
[0124] / / hash mode.
[0125] history:createWebHashHistory(),
[0126] routes
[0127] })
[0128] export const pinia=createPinia()
[0129] / / Set the style attribute of the HTML tag and add all the theme color variables to the inline style const setStyle = (styles) = > {
[0130] if(styles){
[0131] for(let key in styles){
[0132] document.getElementsByTagName('html')[0].style.setProperty(`${key}`,styles[key])
[0133] }
[0134] }
[0135] }
[0136] / / Global front guard, this hook will be triggered before entering each page
[0137] router.beforeEach(async(to,from,next)=>{
[0138] const{theme}=to.query
[0139] const globalStore=useGlobalStore()
[0140] / / Determine the theme color file that needs to be loaded based on the routing parameters
[0141] await import(`@ / style / ${theme}.module.less`).then((module)=>{
[0142] / / module.default is all the color variables exported by the theme color file
[0143] if(module.default){
[0144] / / Global state created by pinia
[0145] globalStore.setTheme(module.default,theme)
[0146] setStyle(module.default)
[0147] }
[0148] next()
[0149] })
[0150] })
[0151] export default routes
[0152] Finally, the content of the theme color file is injected into the style attribute of the HTML tag through the setStyle method. When the theme is dar, the --color-primary in style is #000 in the previous dark.module.less, otherwise it is #0a8cfa in light.module.less, for example:.
[0153] Process 4: After inserting, use the theme color variable directly in CSS. The code is as follows:
[0154] .box{
[0155] background:var(--color-primary)
[0156] }
[0157] Can also be used in HTML
[0158] <div style="color:var(--color-primary)"> Test template usage .
[0159] Process 5: Get the switched theme color variable in JavaScript. The specific code is as follows:
[0160] <script setup>
[0161] import{useGlobalStore}from'@ / store / global'
[0162] const globalStore=useGlobalStore()
[0163] / / 更新后的主题色,获取的是一个对象
[0164] const theme=globalStore.theme
[0165] < / script> .
[0166] Process 6: Use globalStore in Echarts to get the current theme color. If Echarts is used in the project, and the tooltip will also be updated with the theme color change, you will find that the color is not updated with the change of the global globalStore theme. The code is as follows:
[0167]
[0168]
[0169] You need to monitor the themeName of the globalStore. When it changes, re-execute the assignment of chartOption.
[0170]
[0171] In specific applications, the method of this embodiment is as follows: Figure 1 The demonstration interface shown should show a front-end application with a clear theme color distinction, such as the interface of a backend management system. The initial interface is displayed in the default theme color, and the user can switch the theme color by clicking a button or selecting a drop-down menu. After switching, the color of the entire interface, including the navigation bar, menu bar, charts and other elements, should change accordingly to show the effect of the theme color switching.
[0172] Embodiment 2:
[0173] The present invention discloses a front-end theme color dynamic switching system, comprising a theme color file configuration module, a theme color variable reference module and a theme color loading module. The system realizes the front-end theme color dynamic switching through the method disclosed in Example 1.
[0174] The theme color file configuration module is used to perform the following: create a theme color file through the LESS preprocessor, and define a light theme color and a dark theme color.
[0175] As a specific implementation of the theme color file configuration module, this module is used to perform the following operations:
[0176] (1) Configure the Vite configuration file. The default configuration of the additionalData field is the bright theme file;
[0177] (2) Compile and execute the Less file through Vite, write the light theme file light.module.less through Less, configure the light theme color, and write the dark theme file dark.module.less through Less, configure the dark theme color.
[0178] The theme color variable reference module is used to perform the following: configure the global theme file color file through Vite in the Vue project and reference the theme color variable CSS.
[0179] As a specific implementation of the theme color variable reference, this module is used to perform the following:
[0180] (1) Configure the light.module.less file to be referenced globally and import the dark theme file through the additionalData field;
[0181] (2) Compile and execute the vue file through Vite. CSS variables can be referenced in the style tag and script tag of the vue file.
[0182] The theme color loading module is used to perform the following: listen to the parameters in the page URL, dynamically set the theme color style file to a dark theme or a light theme through the routing guard, manage the theme color through the Pinia or Redux state management library, and use it globally. If a third-party library including Echarts is used in the Vue project, obtain and listen to the global theme as dark or light to redraw the chart.
[0183] As a specific implementation of the theme color loading module, the module is used to perform the following:
[0184] (1) Use the route parameter theme to determine whether to use a dark theme or a light theme;
[0185] (2) Create a global store object and export the useGlobalStore method. When the routing parameters change, dynamically update the global store object and set CSS variables. CSS variables can directly use theme color variables.
[0186] (3) Change the CSS variables referenced in the vue file. CSS variables can be referenced in both the style tag and the script tag of the vue file.
[0187] (4) Get the theme color variable after switching in JavaScript;
[0188] (5) In the chart library Echarts, get the current theme color through the theme parameter of the globalStore.
[0189] The present invention has been shown and described in detail above through the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above multiple embodiments, those skilled in the art can know that the means in the above different embodiments can be combined to obtain more embodiments of the present invention, and these embodiments are also within the protection scope of the present invention.
Claims
1. A method for dynamically switching the front-end theme color, characterized in that: The steps include: Theme color file configuration: Create a theme color file through the LESS preprocessor to define light theme colors and dark theme colors; Theme color variable reference: In the Vue project, use Vite to configure the global theme file color file and reference the theme color variable CSS; Theme color loading: listen to the parameters in the page URL, dynamically set the theme color style file to dark theme or light theme through the route guard, manage the theme color through Pinia or Redux state management library, and use it globally. If a third-party library including Echarts is used in the Vue project, obtain and listen to the global theme as dark or light to redraw the chart.
2. The method for dynamically switching the front-end theme color according to claim 1, characterized in that: Theme color file configuration includes the following steps: Configure the Vite configuration file. The default configuration of the additionalData field is the bright theme file. Compile and execute the Less file through Vite, write the light theme file light.module.less through Less, configure the light theme color, and write the dark theme file dark.module.less through Less, configure the dark theme color.
3. The method for dynamically switching the front-end theme color according to claim 1 or 2, characterized in that: The theme color variable reference includes the following steps: Configure the light.module.less file to be referenced globally, and import the dark theme file through the additionalData field; Compile and execute the vue file through Vite, and CSS variables can be referenced in the style tag and script tag of the vue file.
4. The method for dynamically switching the front-end theme color according to any one of claims 1 to 3, characterized in that: Theme color loading includes the following steps: Use the route parameter theme to determine whether the theme color is dark or light. Create a global store object and export the useGlobalStore method. When the routing parameters change, dynamically update the global store object and set CSS variables. CSS variables can directly use theme color variables. Change the CSS variables referenced in the vue file. CSS variables can be referenced in both the style tag and the script tag of the vue file. Get the switched theme color variable in JavaScript; In the chart library Echarts, the current theme color is obtained through the theme parameter of the global globalStore.
5. A front-end theme color dynamic switching system, characterized in that: Used to implement dynamic switching of the front-end theme color by a front-end theme color dynamic switching method as described in any one of claims 1 to 4, the system includes a theme color file configuration module, a theme color variable reference module, and a theme color loading module; The theme color file configuration module is used to perform the following: create a theme color file through the LESS preprocessor, define the light theme color and the dark theme color; The theme color variable reference module is used to perform the following: configure the global theme file color file through Vite in the Vue project and reference the theme color variable CSS; The theme color loading module is used to perform the following: listen to the parameters in the page URL, dynamically set the theme color style file to a dark theme or a light theme through the routing guard, manage the theme color through the Pinia or Redux state management library, and use it globally. If a third-party library including Echarts is used in the Vue project, obtain and listen to the global theme as dark or light to redraw the chart.
6. The front-end theme color dynamic switching system according to claim 5 is characterized in that: The theme color file configuration is used to perform the following: Configure the Vite configuration file. The default configuration of the additionalData field is the bright theme file. Compile and execute the Less file through Vite, write the light theme file light.module.less through Less, configure the light theme color, and write the dark theme file dark.module.less through Less, configure the dark theme color.
7. The front-end theme color dynamic switching system according to claim 5 or 6, characterized in that: The theme color variable reference module is used to perform the following: Configure the light.module.less file to be referenced globally, and import the dark theme file through the additionalData field; Compile and execute the vue file through Vite, and CSS variables can be referenced in the style tag and script tag of the vue file.
8. The front-end theme color dynamic switching system according to any one of claims 5 to 7, characterized in that: The theme color loading module is used to perform the following: Use the route parameter theme to determine whether the theme color is dark or light. Create a global store object and export the useGlobalStore method. When the routing parameters change, dynamically update the global store object and set CSS variables. CSS variables can directly use theme color variables. Change the CSS variables referenced in the vue file. CSS variables can be referenced in both the style tag and the script tag of the vue file. Get the switched theme color variable in JavaScript; In the chart library Echarts, the current theme color is obtained through the theme parameter of the global globalStore.
Citation Information
Patent Citations
Method for realizing web system theme switching based on javaScript
CN115390715A
Theme color switching method and device for hybrid development APP, equipment and medium
CN115495188A
WEB front-end theme mode switching method, equipment and medium
CN115576551A
Universal frame based on Vite
CN116088801A
Method and device for changing color matching theme of large visual screen, medium and equipment
CN116755589A
