반응형
javascript에서 CSS 변수 접근
javascript에서 cs 변수에 접근할 수 있는 방법이 있습니까?여기 내 CSS 변수 선언이 있습니다.
:root {
--color-font-general: #336699;
}
일반적인 방법입니다.
- 계산된 스타일 가져오기:
- 원하는 속성의 값을 가져오는 데 사용합니다.
getComputedStyle(element).getPropertyValue('--color-font-general');
예:
var style = getComputedStyle(document.body)
console.log( style.getPropertyValue('--bar') ) // #336699
console.log( style.getPropertyValue('--baz') ) // calc(2px*2)
:root { --foo:#336699; --bar:var(--foo); --baz:calc(2px*2); }
사용 방법:
window.getComputedStyle(document.documentElement).getPropertyValue('--color-font-general');
이렇게 변경할 수 있습니다.
document.documentElement.style.setProperty('--color-font-general', '#000');
언급URL : https://stackoverflow.com/questions/41725725/access-css-variable-from-javascript
반응형
'programing' 카테고리의 다른 글
각도에서 ng-click not fireing in Angular클릭 시 JS가 실행됩니다. (0) | 2023.09.24 |
---|---|
Wordpress - Woocommerce remove "Added to Cart" 메시지 (0) | 2023.09.24 |
op 사용 시 성능에 미치는 영향 (0) | 2023.09.24 |
jQuery를 사용하여 데이터 속성 값 업데이트 (0) | 2023.09.24 |
문자열에서 워드프레스 쇼트코드 실행 (0) | 2023.09.24 |