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

How to block and prohibit web crawlers in Nginx server

Every website usually encounters many non-search ...

In-depth understanding of the seven communication methods of Vue components

Table of contents 1. props/$emit Introduction Cod...

Analysis of the method of setting up scheduled tasks in mysql

This article uses an example to describe how to s...

The whole process of developing a Google plug-in with vue+element

Simple function: Click the plug-in icon in the up...

A brief discussion on JS regular RegExp object

Table of contents 1. RegExp object 2. Grammar 2.1...

Extract specific file paths in folders based on Linux commands

Recently, there is a need to automatically search...

Detailed explanation of three ways to wrap text in el-table header

Table of contents Problem Description Rendering T...

MySQL 5.6.36 Windows x64 version installation tutorial detailed

1. Target environment Windows 7 64-bit 2. Materia...

Talk about the understanding of CSS attribute margin

1.What is margin? Margin is used to control the sp...

Detailed tutorial on integrating Apache Tomcat with IDEA editor

1. Download the tomcat compressed package from th...

How to split data in MySQL table and database

Table of contents 1. Vertical (longitudinal) slic...

Why the CSS attribute value clear:right does not work in detail

Using the clear property to clear floats is a comm...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...