js控制全局css变量
用 JS 获取全局 css 变量
export const getCssVariableValue = (cssVariableName: string) => {
let cssVariableValue = ""
try {
// 没有拿到值时,会返回空串
cssVariableValue = getComputedStyle(document.documentElement).getPropertyValue(cssVariableName)
} catch (error) {
console.error(error)
}
return cssVariableValue
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
用 JS 设置全局 CSS 变量
export const setCssVariableValue = (cssVariableName: string, cssVariableValue: string) => {
try {
document.documentElement.style.setProperty(cssVariableName, cssVariableValue)
} catch (error) {
console.error(error)
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
上次更新: 2024/10/09, 09:24:28