How to use css variables in JS

How to use css variables in JS

How to use css variables in JS

Use the :export keyword to export a js object in a less/scss file.

$menuText:#bfcbd9;
$menuActiveText:#409EFF;
$subMenuActiveText:#f4f4f5;

// $menuBg:#304156;
$menuBg:#304156;
$menuHover:#263445;

$subMenuBg:#1f2d3d;
$subMenuHover:#001528;

$backWhite:#ffffff;

$sideBarWidth: 210px;

:export {
  menuText: $menuText;
  menuActiveText: $menuActiveText;
  subMenuActiveText: $subMenuActiveText;
  menuBg: $menuBg;
  menuHover: $menuHover;
  subMenuBg: $subMenuBg;
  subMenuHover: $subMenuHover;
  sideBarWidth: $sideBarWidth;
  backWhite: $backWhite;
}

Reference it in the required js file or module.

import style from 'index.scss'
console.log(style.menuText)

vue file

import style from 'index.scss'
export default {
    computed:{
        style(){
            return style
        }
    }
}

Implementation principle

Webpack: Combine with css-loader to enable CSS Modules in your project.

CSS Modules: CSS Modules uses ICSS internally to solve the problems of style import and export. They correspond to the two newly added pseudo-classes: :import and :export.

Appendix: export javascript keyword

Javascript keywords (Reserved Words) refer to those words that have specific meanings in the Javascript language and become part of the Javascript syntax. Javascript keywords cannot be used as variable names or function names. Using Javascript keywords as variable names or function names will cause compilation errors during the Javascript loading process.

Javascript Keywords List:

break , delete , function , return , typeof
case 、 do 、 if 、 switch 、 var
catch , else , in , this , void
continue , false , instanceof , throw , while
debugger , finally , new , true , with
default , for , null , try

Javascript future keywords list:

abstract , double , goto , native , static
boolean, enum, implements, package, super
byte 、 export 、 import 、 private 、 synchronized
char , extends , int , protected , throws
class, final, interface, public, transient
const , float , long , short , volatile

Summarize

This is the end of this article about how to use css variables in JS. For more relevant content about using css variables in JS, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use css var variables in mini programs (so that js can dynamically set css style properties)
  • JavaScript detects whether the browser supports CSS variables code example

<<:  Summary of common knowledge points required for MySQL

>>:  Detailed explanation of how to build a CDN server with Nginx (picture and text)

Recommend

Detailed tutorial on deploying Hadoop cluster using Docker

Recently, I want to build a hadoop test cluster i...

Various methods to implement the prompt function of text box in html

You can use the attribute in HTML5 <input="...

9 Practical Tips for Creating Web Content Pages

Content 1. Give readers a reason to stay. Make the...

Implementation of MySQL master-slave status check

1. Check the synchronization status of A and B da...

Detailed explanation of the use of Vue Smooth DnD, a draggable component of Vue

Table of contents Introduction and Demo API: Cont...

How to implement distributed transactions in MySQL XA

Table of contents Preface XA Protocol How to impl...

Detailed example of deploying Nginx+Apache dynamic and static separation

Introduction to Nginx dynamic and static separati...

The main differences between MySQL 4.1/5.0/5.1/5.5/5.6

Some command differences between versions: show i...

View the dependent libraries of so or executable programs under linux

View the dependent libraries of so or executable ...

XHTML three document type declarations

XHTML defines three document type declarations. T...

MySQL detailed summary of commonly used functions

Table of contents MySQL Common Functions 1. Numer...