-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale_css_variables.js
83 lines (72 loc) · 2.84 KB
/
scale_css_variables.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function scaleCSSVar(docQueryResult,cssVarName,factor,iPrecision) {
// docQueryResult is a variable created by a document.querySelector() call,
// e.g. var rootcss1 = document.querySelector(':root');
// changes --my-heightvar for example from 3.6em to 4.12em, depending on factor
// set iPrecision to zero if you want to round to integers
var rs = getComputedStyle(docQueryResult);
if (rs) {
var str = rs.getPropertyValue(cssVarName);
if (str && (str.length > 0)) {
var patt1 = /[0-9.]/g;
var patt2 = /[a-zA-Z]/g;
var letters_inspec = str.match(patt2).join('');
var digits_inspec = str.match(patt1).join('');
if ((parseFloat(digits_inspec) > 0.0) && (factor > 0)) {
var newDigits = parseFloat(digits_inspec) * factor;
if (newDigits > 0) {
var newStr = '' + newDigits.toFixed(iPrecision) + letters_inspec;
//console.log(newStr);
docQueryResult.style.setProperty(cssVarName,newStr);
}
}
}
}
}
function setCSSVar(docQueryResult,cssVarName,newValue,newUnits) {
// if newUnits is omitted, it uses existing units e.g. "px"
// css variable must already exist
var rs = getComputedStyle(docQueryResult);
if (rs) {
var str = rs.getPropertyValue(cssVarName);
if (str && (str.length > 0)) {
var patt2 = /[a-zA-Z]/g;
if (newUnits && (newUnits.length > 0))
var letters_inspec = newUnits;
else
var letters_inspec = str.match(patt2).join('');
if (newValue && ((newValue > 0) || (newValue.length > 0))) {
var newStr = '' + newValue + letters_inspec;
//console.log(newStr);
docQueryResult.style.setProperty(cssVarName,newStr);
}
}
}
}
function getCSSVarUnits(docQueryResult,cssVarName) { var retVal = null;
// e.g. "px"
var rs = getComputedStyle(docQueryResult);
if (rs) {
var str = rs.getPropertyValue(cssVarName);
if (str && (str.length > 0)) {
var patt2 = /[a-zA-Z]/g;
var letters_inspec = str.match(patt2).join('');
if (letters_inspec)
retVal = letters_inspec;
}
}
return retVal;
}
function getCSSVarValue(docQueryResult,cssVarName) { var retVal = null;
// returns a string e.g. "40" , use parseFloat() on result if you want a number
var rs = getComputedStyle(docQueryResult);
if (rs) {
var str = rs.getPropertyValue(cssVarName);
if (str && (str.length > 0)) {
var patt1 = /[0-9.]/g;
var value_inspec = str.match(patt1).join('');
if (value_inspec)
retVal = value_inspec;
}
}
return retVal;
}