your programing

자바 스크립트에서 CSS 변수에 액세스

lovepro 2020. 12. 29. 08:06
반응형

자바 스크립트에서 CSS 변수에 액세스


이 질문에 이미 답변이 있습니다.

자바 스크립트에서 CSS 변수에 액세스하는 방법이 있습니까? 여기 내 CSS 변수 선언.

:root{
    --color-font-general:#336699;
}

표준 방식 :

  1. 계산 된 스타일 얻기 getComputedStyle
  2. getPropertyValue원하는 속성의 값을 가져 오는 데 사용 합니다.
getComputedStyle(element).getPropertyValue('--color-font-general');

예:

var style = getComputedStyle(document.body);
console.log(style.getPropertyValue('--color-font-general'));
:root { --color-font-general: #336699; }


이것을 사용하십시오 :

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

반응형