L
L
lillianfisher2021-05-13 01:24:18
JavaScript
lillianfisher, 2021-05-13 01:24:18

How to use scss variable in js?

Hello, I have a lot of scss constants and I need to set styles in js whose values ​​are equal to scss constants. Is there a way to specify the value of the scss constant in js and not calculate it yourself ...
For example
$pi: 3.1415926535897932384626433832795;

document.getElementByID("el").style.opacity = "  `require(scss).$pi` / 6 "

The value must be substituted at compile time

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Dubrovin, 2021-05-13
@alekcena

Not directly.
Just invented hack.

:root {
--test: 'Мегаполезная переменная которую нельзя просто запихнуть в js'
}
- css ( https://developer.mozilla.org/ru/docs/Web/CSS/:root ) stuff our constants. which we want to use further.
We connect styles at the beginning, js later.
Getting the value
document.querySelector(':root').attributeStyleMap.get('--test');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question