getVariable(domElement, '--example')
// 'hello'
getVariable(domElement, '--example', (v) => parseInt(v, 36))
// 29234652
Last active
September 6, 2020 18:53
-
-
Save sergeysova/f1f4d34c59514201494f99c98291185e to your computer and use it in GitHub Desktop.
Read CSS variable from JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getVariable(element, variable, reader = String) { | |
| const style = window.getComputedStyle(element) | |
| return reader(style.getPropertyValue(variable).trim()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment