css 變數與javascript結合

來源:互聯網
上載者:User

標籤:function   寫入   tco   root   com   screen   etc   element   ons   

<div onClick="test(‘yellow‘)">
CSS Variable
</div>

================CSS

:root{
--mainColor:red;
}

div{
background:black;
color:var(--mainColor);
}

div{
text-align:center;
}

:root {
--mainWidth:1000px;
--leftMargin:100px;
}

.main {
width: var(--mainWidth);
margin-left: var(--leftMargin);
}

@media screen and (min-width:1480px) {
:root {
--mainWidth:800px;
--leftMargin:50px;
}
}

================javascript


// 讀取
var root = getComputedStyle(document.documentElement);
var cssVariable = root.getPropertyValue(‘--mainColor‘).trim();

console.log(cssVariable); // ‘75px‘

// 寫入

function test(m){
document.documentElement.style.setProperty(‘--mainColor‘, m);
}

css 變數與javascript結合

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.